Clean JavaScript code in your views

Comparing #1 with #2

Revision #2 was created by nineinchnick nineinchnick on Aug 30, 2013, 8:28:00 PM.

fixed typos, adjusted first paragraph

Content

This tutorial presents a way of separating JS code from views and passing to it values from PHP.
 
 
Yii provides two handful ways of keeping JavaScript code close to the widgets and other elements they interact with:

* strings prefixed with 'js:'
[...]
"use strict";
// private var
var _settings;
 
    // public var
 
    yourPluginName.someProperty = 'default value';
// public method appEdityourPluginName.init = function(settings) {
_settings = $.extend({}, settings);
}
[...]
```

The '_settings' var is private and can be only referenced inside functions defined in yourPluginName;
 
 
Now your PHP and JS code is: * separated and cleaner * safer * easier to read * more extensible (no more hardcoded labels and urls in JS!)