I often use an "html bridge". I dont know if it is a good practice or if
there is a better one.
The variables are printed in the layout into hidden inputs, and javascript
loads them from there. I use it to put what the aplication needs, you can
use it to pass the controller name. Here is the code:

In the php file :

<?
/**
 * HSB - Html Javascript Bridge / Puente javascript - html
 */
if (isset($sf_context)) {
  echo '<input class="hsb" id="hsb_sf_enviroment" name="sf_enviroment"
value="'.$sf_context->getConfiguration()->getEnvironment().'"
type="hidden">';
  echo '<input class="hsb" id="hsb_sf_app" name="sf_app"
value="'.$sf_context->getConfiguration()->getApplication().'"
type="hidden">';
  echo '<input class="hsb" id="hsb_sf_action" name="sf_action"
value="'.$sf_context->getActionName().'" type="hidden">';

}
?>

In the js file :

function getHSB(name, default_value) {
   default_value = default_value === undefined || default_value === null ?
false : default_value;
   var d = $('body').data('hsb');
   return d[name] ? d[name] : default_value;
}

On Mon, Aug 9, 2010 at 2:16 AM, Sebastien Armand [Pink] <khe...@gmail.com>wrote:

> In both your suggestions, if I get them correctly, the idea is that from a
> template, I should output some javascript to set some variables or any other
> way to get the url back from my javascript later. Which seems weird to me,
> and "wrong" on the side that I don't really want to add js in my templates
> and prefer to keep it separate.
>
> For an idea, my case goes like this: I have a form to enter some
> information of a sofa for example, and one of the fields allows me to chose
> a "design" for the sofa. This design has some default dimensions: width and
> length. Those are the information I want to bring back through ajax.
>
> So:
> 1. get to the form page to create a new sofa
> 2. choose a design
> 3. this triggers the JS to go look for the default size (from the design
> module) and fill it in the form.
>
> my js file is in the js folder and in it I have a url linking to
> "/design/:design_id.json" but for this to work on my dev environment, I need
> that url to be: "/frontend_dev.php/design/:design_id.json".
>
> So is there a way to output that url in the js through php and symfony?
> Or a way that I can get it through the dom but without already having to
> put it here by myself beforehand?
>
>
>
> 2010/8/7 Stéphane <stephane.er...@gmail.com>
>
> It shouldnt break.
>> You should do this:
>>
>> Create a route to reach your module/action (/apps/$app/config/routing.yml,
>> check doc).
>> Then in a template, write something like:
>>
>> <script type="text/javascript">
>> window.myactionurl = "<?php echo url_for('@my_route')?>";
>>
>> alert(window.myactionurl);
>> $.post(window.myactionurl, null, function(data){//...});
>> </script>
>>
>> This is a stupid example (about the window.myactionurl, store this
>> somewhere else).
>>
>>
>> Before Printing, Think about Your Environmental Responsibility!
>> Avant d'Imprimer, Pensez à Votre Responsabilitée Environnementale!
>>
>>
>>
>> On Sat, Aug 7, 2010 at 12:25 PM, Phennim <phen...@gmail.com> wrote:
>>
>>> Get the url from the DOM using js.
>>>
>>> $('a.do-ajax').click(function(){
>>>  var url = $(this).attr('href');
>>>
>>>  $.post(url, function(data){
>>>    //do stuff
>>>  });
>>> });
>>>
>>>
>>> On Aug 7, 11:11 am, "Sebastien Armand [Pink]" <khe...@gmail.com>
>>> wrote:
>>> > Hello everyone,
>>> >
>>> > I was wondering how to get an environment independent url in javascript
>>> > files. My js needs to connect to the server and get some JSON data and
>>> > update a form on certain actions. But putting that actions url in the
>>> js
>>> > directly breaks everything when you change environment...
>>> >
>>> > I might be missing something here but don't see the light of how to do
>>> it,
>>> > any insight welcome ;-)
>>>
>>> --
>>> If you want to report a vulnerability issue on symfony, please send it to
>>> security at symfony-project.com
>>>
>>> You received this message because you are subscribed to the Google
>>> Groups "symfony users" group.
>>> To post to this group, send email to symfony-users@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> symfony-users+unsubscr...@googlegroups.com<symfony-users%2bunsubscr...@googlegroups.com>
>>> For more options, visit this group at
>>> http://groups.google.com/group/symfony-users?hl=en
>>>
>>
>>  --
>> If you want to report a vulnerability issue on symfony, please send it to
>> security at symfony-project.com
>>
>> You received this message because you are subscribed to the Google
>> Groups "symfony users" group.
>> To post to this group, send email to symfony-users@googlegroups.com
>> To unsubscribe from this group, send email to
>> symfony-users+unsubscr...@googlegroups.com<symfony-users%2bunsubscr...@googlegroups.com>
>> For more options, visit this group at
>> http://groups.google.com/group/symfony-users?hl=en
>>
>
>  --
> If you want to report a vulnerability issue on symfony, please send it to
> security at symfony-project.com
>
> You received this message because you are subscribed to the Google
> Groups "symfony users" group.
> To post to this group, send email to symfony-users@googlegroups.com
> To unsubscribe from this group, send email to
> symfony-users+unsubscr...@googlegroups.com<symfony-users%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/symfony-users?hl=en
>

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to