Method js() will return jQuery_Chain object which would record all calls
to it's non-existant methods and convert them into jQuery call chain.
js([action], [other_chain]);
Action can represent javascript event, such as "click" or "mouseenter".
If you specify action = true, then the event will ALWAYS be executed on
pageload. It will also be executed if respective view is being reloaded
by js()->reload()
(Do not make mistake by specifying "true" instead of true)
action = false will still return jQuery chain but will not bind it.
You can bind it by passing to a different object's js() call as 2nd
argument or output the chain in response to AJAX-ec call by calling
execute() method.
1. Calling with arguments:
$view->js(); // does nothing
$a = $view->js()->hide(); // creates chain for hiding $view but does not
bind to event yet.
2. Binding existing chains
$img->js('mouseenter', $a); // binds previously defined chain to event on
event of $img.
Produced code: $('#img_id').click(function(ev){ ev.preventDefault();
$('view1').hide(); });
3. $button->js('click',$form->js()->submit());
clicking button will result in form submit
4. $view->js(true)->find('.current')->text($text);
Will convert calls to jQuery chain into JavaScript string:
$('#view').find('.current').text('abc'); // The $text will be json-encoded
to avoid JS injection.
5. ON YOUR OWN RISK
$view->js(true,'alert(123)');
Will inject javascript un-escaped portion of javascript into chain.
If you need to have a custom script then put it into file instead,
save into templates/js/myfile.js and then include:
$view->js()->_load('myfile');
It's highly suggested to bind your libraries with jQuery namespace by
registered them as plugins, this way you can call your function easily:
$view->js(true)->_load('myfile')->myplugin('myfunc',array($arg,$arg));
This approach is compatible with jQuery UI Widget factory and will keep
your code clean