Difference between #2 and #3 of
Clean JavaScript code in your views

Revision #3 has been created by David Newcomb on Nov 27, 2014, 1:42:04 PM with the memo:

correct grammer and poor English
« previous (#2) next (#4) »


Title unchanged

Clean JavaScript code in your views

Category unchanged


Yii version unchanged

Tags unchanged

javascript, registerScript, CClientScript, js

Content changed

This tutorial presents a way of separating JS code from views and passing to it values from PHP. Yii provides two handelpful ways of keeping JavaScript code close to the widgets and other elements they interact with: * strings prefixed with 'js:' * CClientScript.registerScript method Quickly, small snippets of JavaScript code turn into big ugly strings filled with PHP variables and without proper syntax highlighting.

This tutorial shows a method of organizing JavaScript code and integrating it with a PHP backend.
* the jQuery object

This allows
you to extend the global yourPluginName object by adding methods and properties to it, keeping them all in one scope. Now you can place your bulky JS code from your views inside suchthis plugin and use them as:

# Namespaced code

By putting all your code in
suchthis plugin you keep them in a limited scope and thus create a namespace for it. That helps avoiding name conflicts and keeps your code cleaner. Another important feature is that suchthis plugin can be registered many times. Sometimes thatis can happen when you load an action through AJAX on a page that has already registered thate script. 

This also allows extending if further, adding more functions.

Also, since there actually is a limited scope, strict mode can be enabled:
18 0
Viewed: 37 553 times
Version: 1.1
Category: Tutorials
Written by: nineinchnick
Last updated by: samdark
Created on: Aug 30, 2013
Last updated: 2 years ago
Update Article


View all history