EJsWidget
EJsWidget a behavior class providing a structured client-side behavior representation for Yii widgets.
It also includes an experimental part supporting "self actions" of widgets, by making the controllers' "action provider" ability more handy.
See EJsWidget in action below:
class ETestWidget extends CWidget { public $name; public function init() { parent::init(); EJsWidgetBehavior::prepare($this); $this->render('index'); } public function jsAttributes() { return array_merge(parent::jsAttributes(), array( 'name' )); } }
- ECalculatorWidget.php
- index.php (widget main view)
- ecalculatorwidget.js
- rendering the widget
- DemoController
class ECalculatorWidget extends ESimpleActionWidget { public $num1 = 0; public $num2 = 0; /** * INitialization **/ public function init($widgetAction = false) { parent::init(); //Full widget initialization IF the current request isn't a self-request if (!$widgetAction) { EJsWidgetBehavior::prepare($this); $this->render('index'); } } /** * Defining which params to be passed to the js instance - these ones will be recent in the widget's "opt" object * Can be set only widget attribute names or arbitrary name=>value pairs **/ public function jsAttributes() { return array_merge(parent::jsAttributes(), array( 'num1','num2' )); } /** * UI helpers **/ public function num1() { return CHtml::textField($this->id . '_num1', $this->num1); } public function num2() { return CHtml::textField($this->id . '_num2', $this->num2); } public function bAdd() { return CHtml::button('add', array( 'id' => $this->id . '_add', 'onclick'=>$this->jsObj() . '.add()' )); } /** * Widget self-actions * **/ public function actionAdd() { echo (int)$this->num1 + (int)$this->num2; } }
- EFancyCalculatorWidget.php
- index.php (widget main view)
- ecalculatorwidget.js
- ecalculatorwidget.css
- rendering
- DemoController
require_once(__DIR__ . '/../ecalculator/ECalculatorWidget.php'); class EFancyCalculatorWidget extends ECalculatorWidget { public static function getJsName() { return 'efancycalculatorwidget.js'; } public function init($portletAction = false) { parent::init($portletAction); } //UI public function bMult() { return CHtml::button('mult', array( 'id' => $this->id . '_mult', 'onclick'=>$this->jsObj() . '.mult()' )); } //Actions public function actionMult() { echo (int)$this->num1 * (int)$this->num2; } }
Learn, download (demo source, including the library), comment and rate it on the Yii EJsWidget page.