How to generate URL for other pages using JavaScript in Symfony2? - javascript

Within my Symfony2 application, in order to link to other pages, I've generated the href value using the path Twig function.
I maybe wrong but I think I previously read about a JavaScript equivalent but can't seem to find it now. I am generating some html using JavaScript and want to know if its possible to generate the href values to other pages within the application using purely JavaScript.
Appreciate any advice.

You are probably looking for this bundle: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle
Then you can do this kind of stuff in javascript:
Routing.generate('my_route', {"my":"param"});

I would recommend using FosJsRoutingBundle.
Lets assume an action of controller:
/**
* #Route("/indx/", options={"expose"=true}, name="indexRoute")
* #Template()
*
* #return array
*/
public function indexAction()
{
(...)
}
options={"expose"=true} is what is important here.
Now js equivalent of path call is just:
Routing.generate('indexRoute');

Related

Take C# const from backend and use it in JS file?

I have one tricky question.
Is there a way to take C# const and use it in .JS script with Jquery ?
This is how const look:
public class UserRoles
{
public const string Read = "Read";
public const string ReadWrite = "ReadWrite";
}
It depends a bit of what you are trying to do with those values.
You could place the values in a js object when your UI initializes (ex: window.YourAppName.Constants.Read = "your C# constant" in Index.html). Then you could load your jquery script and make use of the constant values once the document finished loading.
Alternatively, if you are using MVC, you can make use of tags within your views, and thus have access to C# code (viewmodel, enums, etc.). However, if you have lots of js code then it would be best to keep that in js files and in such a case I would go for the first option.
You can't use Razor in JavaScript files, you would have to have the variable passed to a razor view in the viewbag/data or model.
Then in the shared layout you could create a javascript function that returns this variable, then in your .JS file you could call that function to get the variable as long as it is loaded after.

How to put external js API in cocos creator?

I am just wondering how to put another js API library( in my case, i want to put the smartfoxserver javascript API library) in cocos creator? because what i did in cocos2d-js, i just need to add it in project.json, and I am wondering if i can do same way in cocos creator or another way?
thank you in advance
reference question from:
http://discuss.cocos2d-x.org/t/how-to-put-another-js-api-library-in-cocos-creator/32598
good day
you can use module exports
var x = {
/*
your stuff
*/
};
module.exports = x;
then access it from other scripts using
var externalScript=require("name of the script with module.exports");
cheers
i just scan the smartfoxserver. it use the websocket that is fine just drag an drop the SFS2X_API_JS.js to the project and require it and init it maybe you would use window.xxx to set the connection handler as global value

Laravel string localization and AngularJS

So recently i started to convert my jQuery stuff to AngularJS, but there was a solution what i did not think about.
Not all my pages will use Angular, and i am using Laravel's built in localization.
So the views used by Angular are just plain html, no php or blade included.
So i do not really want to mix the 2 in a view.
Where i use blade i use the built in function example: {{ trans('user.first_name') }}
But since i don't want to mix, and my AngularJS views are pure html, i do not want to use it this way <?php echo trans('user.first_name') ?> in my Angular view.
So i was thinking to include Angular localization for AngularJS, but maintaining 2 different localization structure will be a pain and building a good logic for it is another pain.
So anybody with a solution? currently i am clueless. Because for example with jQuery i did not had to worry about this, because it was just dom manipulation, but now its is more complex
I have developed a package that create a JavaScript file from all Laravel's messages. I think you could use it to have access directly in Laravel.
Basically, after installing the package and running the bundled command you will have a global object: windows.Lang. From there you can do different basic operations such as:
Get a localized message
var message = Lang.get('messages.first_name');
Get a localized message with replacements
var message = Lang.get('messages.welcome', { name: 'Joe' });
Change the current locale (applies to JavaScript)
Lang.setLocale('es');
You can find more information: https://github.com/rmariuzzo/laravel-js-localization
Solved this with a ViewComposer:
app/Http/ViewComposers/TranslateComposer.php
<?php
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class TranslateComposer
{
/**
* Bind data to the view.
*
* #param View $view
* #return void
*/
public function compose(View $view)
{
$view->with('jstrans', json_encode(trans('my_locale_resource')));
}
}
app/Providers/AppServiceProvider.php
public function boot()
{
view()->composer(
'*', 'App\Http\ViewComposers\TranslateComposer'
);
}
resources/views/index.blade.php
<script type="text/javascript">
var trans = JSON.parse('{!! $jstrans !!}');
</script>

ActiveCollab - How to make an ajax call from a custom module on page load

I'm trying to initiate an ajax call on project brief page by adding a javascript file.I need to display some extra information along with existing project brief information. I included a javascript file in a custom module and followed the procedure explained below. Can you correct me If I'm missing anything here.
I used $.ajax({}) to call the ajax request and my code is like this
$.ajax(
{
type: "post",
url: "index.php?path_info=projectfieldsextra",
'data' : { 'client_value' : 178},
success: function(el)
{
alert(el);
return false;
//$("#project").html(el);
}
});
In controller I created a function get_project_information() and in module definition class I created a route like this
Router::map('projectfieldsextra', 'projectfieldsextra', array('controller' => 'project_fields', 'action' => 'get_project_information'));
But while makinf call , it is giving me an error like this -
Call to a member function isInlineCall() on a non-object in /opt/lampp/htdocs/activecollab/activecollab/4.0.13/angie/frameworks/environment/controllers/FwApplicationController.class.php on line 211
Could anyone help me out in this ?
Thanks in advance
Jayesh
My best guess is that your custom controller (ProjectFieldsController) is not properly set up (it is not properly accepting construction parameters and transferring them to the parent class). If you are overriding controller's constructor, you should do it like this:
/**
* Controller constructor
*
* #param Request $parent
* #param mixed $context
*/
function __construct($parent, $context = null) {
parent::__construct($parent, $context);
// Your code
}
Note that there are other problems with your code, but they are not part of the question, so I will mention them just briefly:
Don't hack the system module. That's a recipe for upgrade problems. Write your own module that hooks into the system via supported events instead.
System convention is to use underscore notation for various system names, including route names. show_project_info is much more readable than showprojectinfo. This is not an error, but you will get along with the framework much easier if you follow the existing naming conventions.

Call a JavaScript function from C++

I have a CDHTMLDialog, with which I have 2 HTML pages and a .js file with a few fairly simple functions.
I would like to be able to call one of the JS functions from my program with a simple data type passed with it. e.g. MyFunc(int). Nothing needs to be returned.
I would appreciate any guidance on how I go about this,
thanks.
Edit: Thanks to CR for his answer, and everyone else who submitted there ideas too.
Something a little like this worked in the end (stripped a little error handling from it for clarity):
void callJavaScriptFunc(int Fruit)
{
HRESULT hRes;
CString FuncStr;
CString LangStr = "javascript";
VARIANT vEmpty = {0};
CComPtr<IHTMLDocument2> HTML2Doc;
CComPtr<IHTMLWindow2> HTML2Wind;
hRes = GetDHtmlDocument(&HTML2Doc);
hRes = HTML2Doc->get_parentWindow(&HTML2Wind);
if( Fruit > 0 )
{
FuncStr = "myFunc(808)"; // Javascript parameters can be used
hRes = HTML2Wind->execScript(FuncStr.AllocSysString(), LangStr.AllocSysString(), &vEmpty);
}
}
Easiest approach would be to use the execScript() method in the IHTMLWindow2 interface.
So you could get the IHTMLDocument2 interface from your CDHTMLDialog by calling GetDHtmlDocument, then get the parentWindow from IHTMLDocument2. The parent window will have the IHTMLWindow2 interface that supports execScript().
There might be an easier way to get the IHTMLWindow2 interface from your CDHTMLDialog but I'm used to working at a lower level.
the SpiderMonkey library can "Call a JavaScript function from C++", please refer to
http://egachine.berlios.de/embedding-sm-best-practice/ar01s02.html#id2464522
but in your case, maybe this is not the answer.
To give you a hint - javascript injection in server-side-technologies is usually performed through bulk-load at startup (GWT) or injected when the HTML is generated and served each post-back (ASP.NET).
The important point of both approaches is that they inject the javascript calls somewhere in the page (or in a separated .js file linked in the HTML in case of GWT) when generating the HTML page.
Even if you're on win development (looks like it since you're on MFCs) it might be the case that you have to insert your js method call in the HTML and then load (or reload if you wish to interact with the html from your MFC app) the HTML file in your CHTMLDialog.
I don't see any other way of achieving this (maybe I am just not aware of some suitable out-of-the-box functionality) other than editing your HTML and (re)loading it - which is pretty convenient and workable if you have to call your js method once off or just inject some kind of event-handling logic.
Might be a bit of a pain if you have to interact with the page from your MFC app. In this case you have to re-generate your HTML and reload it in your CHTMLDialog.
Either way you can simply have some kind of placeholder in your HTML file, look for that and replace with your javascript code, then load the page in your CHTMLDialog:
onclick="__my_Javascript_Call_HERE__"

Categories

Resources