AbstractView::js PHP Метод

js() публичный Метод

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
public js ( string | boolean | null $when = null, array | jQuery_Chain | string $code = null, string $instance = null ) : jQuery_Chain
$when string | boolean | null Event when chain will be executed
$code array | jQuery_Chain | string JavaScript chain(s) or code
$instance string Obsolete
Результат jQuery_Chain
    public function js($when = null, $code = null, $instance = null)
    {
        // Create new jQuery_Chain object
        if (!isset($this->app->jquery)) {
            throw new BaseException('requires jQuery or jUI support');
        }
        /** @type App_Web $this->app */
        // Substitute $when to make it better work as a array key
        if ($when === true) {
            $when = 'always';
        }
        if ($when === false || $when === null) {
            $when = 'never';
        }
        if ($instance !== null && isset($this->js[$when][$instance])) {
            $js = $this->js[$when][$instance];
        } else {
            $js = $this->app->jquery->chain($this);
        }
        if ($code) {
            $js->_prepend($code);
        }
        if ($instance !== null) {
            $this->js[$when][$instance] = $js;
        } else {
            $this->js[$when][] = $js;
        }
        return $js;
    }