Wednesday, February 28th, 2007
Symfony Unobtrusive JavaScript Plug-In
<p>François Zaninotto has been talking about a new Unobtrusive JavaScript Plug-In for symfony.Simple Example
-
-
<div id="foobar">
-
I'm here !
-
</div>
-
<?php UJS("$('#foobar').css('display', 'none')") ?>
-
How it is generated
-
-
<?php UJS_write('<a href="#" onclick="$('#foobar').toggle();return false;">click me') ?>
-
<div id="foobar">
-
I'm here !
-
</div>
-
<?php UJS_change_style('#foobar', 'display:none') ?>
-
this becomes...
-
-
<span style="display: none" class="UJS_placeholder" id="UJS_0"></span>
-
<div id="foobar">
-
I'm here !
-
</div>
-
...
-
// <![CDATA[
-
$().ready(function(){
-
$('#UJS_0').after('<a href="#" onclick="$('#foobar').toggle();return false;">click me');
-
$('#UJS_0').remove();
-
$('#foobar').css('display', 'none');
-
})
-
// ]]>
-
</script>
-
Related Content:











Nowhere on the Symfony site does it mention the fact that this plugin relies on jQuery. Come on guys, credit where it’s due.
“This implementation uses JQuery as the underlying JavaScript framework, but the same could be achieved with Prototype.”
@Dustin – I’ve looked and I still can’t find the text you quoted. Perhaps you should make your credits clearer?
Dean,
It’s the last phrase in the Introduction section in the Trace page for the plugin.
Still, seeing that it’s still alpha, I’ll wait a while before trying it out.
@Dean – I am referencing the last sentence of the introduction paragraph on the trac plugin page.
OK, I missed it. Still, a link would be good. :-)
How is this “Unobtrusive” It inserts a bunch of JS code into the middle of the HTML body.
Well it’s unobtrusive because the page is still completely functional without the Javascript. Unobtrusiveness has nothing to do with including javascript in the body. Anyway it would be unpragmatic to try to place all your JS in external files.
lol
@Florin, I always thought that word for what you are talking about is “degradable”. Check dictionary for meaning of the word “unobtrusive” : http://dictionary.reference.com/browse/unobtrusive
With frameworks like jQuery it is so easy to keep all javascripts in external files and I don’t understand why it is unpragmatic.
Dean,
It’s the last phrase in the Introduction section in the Trace page for the plugin.
Still, seeing that it’s still alpha, I’ll wait a while before trying it out.
I’ve tryed the sfJqueryPlugin from http://trac.symfony-project.com/wiki/sfJqueryPlugin wich is supposed to replace Javascript helper with the same helpers using jQuery, so no need to use prototype anymore, and it works very good