Yii 1.1: chainable

Behavior adds fluent interface to any Yii components
3 followers

About

EChainableBehavior is Yii extension that gives posibility to use jQuery-like chainable style of working with public component properties, class methods that not return important data or AR attributes (fluent interface).

Installation

Place EChainableBehavior.php to your extension directory (yourWebRoot/protected/extensions)

Usage

example:

$user->attachBehavior('chain',array('class'=>'ext.EChainableBehavior'));
    $id = $user->chain      
       ->email('mail@example.com') # set $user->email
       ->firstName('Ivan')         # set $user->firstName
       ->lastName('Ivanov')
       ->id();                     # if no attributes passed then return value

It's convenient to use with clientScript to make code of views more clean:

$res=Yii::app()->clientScript->chain
       ->registerScriptFile('script1.js')
       ->registerScriptFile('script2.js')
       ->registerScriptFile('script3.js')
       ->coreScriptUrl(); //will return clientScript->coreScriptUrl

Before using of chainable behavior you need to configure this component in your application configuration:

'components'=>array(
   'clientScript'=>array(   
           'behaviors'=>array(
               'chain'=>'ext.EChainableBehavior'
           )
    ),
 ....
 ),

If last parameter passed to method is equal to constant ECB_RETURN then method result will returned instead of $this

Changelog

Nov 11 2010: Support of calling owner methods, refactoring

Nov 4 2010: Initial release

License

New BSD license

author: Vitaliy Stepanenko mail@vitaliy.in

Resources

Total 1 comment

#2101 report it
Nayjest at 2010/11/11 12:07pm
:)

Example with clientScript isn't actual now, because Qiang added 'return $this' to methods of CClientScript mega-operativley after reading my issue in google.code :)

Leave a comment

Please to leave your comment.

Create extension