There are several decisions to make here really:

Adding it to a global js file is a good way to go, especially if you
have a far-future expires header because then the extra "bloat" is not
an issue. However if you have lots of template specific code there
then it may be a bit out of place, for example if you have a very
specific click handler for an element on one page, much like the
symfony Javascript functions.

Adding to a separate js file is also one way to go, but then I would
only do this if the users loading one js file are not the same users
as would be loading the other one (for example admin users versus
normal users). Otherwise using a separate js file adds an unnecessary
http request when users are likely to download both. It is better in
this case to just have one js file and then the user's browser can
cache it.

Embedding it in the template may well be the best way to go, you just
need to weigh it up. I always try to make functions generic enough
that they can go in a global file first, then resort to adding them to
the specific partial or template if/when they get very template
specific. I totally don't see a problem with this, however it's
recommended to use an observer that wraps your js code so it is only
triggered when the window has finished loading, that way you will not
be adding overhead to the page loading time.

If you are worried about the final markup and the notion of having
"scattered" javascript in throughout your html, then you can easily
solve that with slots, or with some kind of custom function replacing
the symfony javascript helper, such as add_js_to_footer() which
collects all the js as the templates are rendered and splats them out
just before the final </ body>

Lots of things to consider, and depending on the case, no true right
or wrong answer :)

On Jun 1, 6:01 pm, Mark Smith <[email protected]> wrote:
> If I am writing some page specific javascript where should I put it?
>
> The way I see it I have 3 options:
>
> 1) Embed it in the page template (feels very hacky)
>
> 2) Stick it in a global .js and load it every time regardless. (could
> cause problems and isn't really scalable)
>
> 3) Put it in its own .js file in the web folder and add it to the view
> (again seems like it would be hard to manage, but feels slightly less
> evil than the first 2 options)
>
> Are there other options I haven't considered?
>
> What do you do?
>
> Thanks

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

Reply via email to