Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (87 sloc) 3.2 KB

Jii 0.0.4

Javascript library for Yii You can use it to convert PHP variables (numbers, strings, booleans, array, objects) to their Javascript equivalents. A jii object will be created on the javascript global scope and it will contain everything you add.

Configuring Yii

Copy Jii directory to /path/to/application/protected/components and add the following lines to your config/main.php:

    'components' => array(
	    'jii' => array(
		    'class' => 'application.components.Jii.Jii',
		    	// uncomment the following if you do not want to use the minified version
		    	// 'script' => 'jii-0.0.4.js',

Add Jii to your page

Adding Jii to your page can be done as follows:

	Yii::app()->clientScript->registerScript('jii', Yii::app()->jii->getScript(), CClientScript::POS_END);

Knockout js support is automatically added to Jii and includes the following functions:

// each function accept one Model as argument

Javascript Models

On the client side, each model that you add with Yii::app()->jii->addModel() will expose the following methods:

 * Finds one model instance based on the specified attribute
 * @param object {attribute: "attribute_name", value: attribute_value
 * @return object or null if none object is found

// the json representation of your model

// the number of model instances found

Direct model or data provider jsonization

You can directly encode models - and their relations - as well as data provider in the following way:

$model = new Model();
Yii::app()->jii->addModel('model', $model);

You can add params, models, urls or functions as follows. Notice that type casting from PHP to Javascript is available only for params.

Sample usage

	// adding params
	Yii::app()->jii->addParam('integer', 10);

	// adding urls
	Yii::app()->jii->addUrl('view_test_url', $this->createUrl('test/view', array('id' => 1)));

	// adding functions
	Yii::app()->jii->addFunction('function', 'function(){ alert("This is an alert!"); }');

Binding functions (since 0.0.4)

You can add functions that will be executed once the page has finished loading in the followin way:

// the following function will be called once the document is ready
	alert("Page loading has finished");

Adding CActiveRecord to Jii

As regards CActiveRecord instances (or class instances inheriting from CModel), you can select which attribute you want Javascript have access to by adding the following method to each of the CActiveRecord class files you wish to convert ...

	public function getJsonizeables()
		return array(

... and then you can use the following code to add it to Jii (support for CActiveDataProvider has been added):

	$jsonized_model = Yii::app()->jii->jsonize($model);

	// adding models to jii
	Yii::app()->jii->addModel('model', $jsonized_model);