Push data from PHP controller to global JS variable (inspired by https://github.com/gazay/gon)
Via Composer
$ composer require ijackua/yii2-gon
Add component to application config
'components' => array(
'gon' => 'ijackua\gon\GonComponent'
),
And to app bootstrap
section
$config = array(
'bootstrap' => array('gon'),
...
Full component configuration example
'components' => array(
'gon' => array(
'class' => 'ijackua\gon\GonComponent',
'jsVariableName' => 'gon',
'globalData' => ['g1' => 1, 'g2' => '2'],
'showEmptyVar' => true,
)
),
Anywhere in your app push
key -> value
\Yii::$app->gon->push('someObj', ['a'=>'b']);
\Yii::$app->gon->push('str', 'hello');
On JS side you will get
> window.gon
>> Object
someObj: Object
{
a: "b"
}
str: "hello"
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
All Notable changes to ijackua/yii2-gon
will be documented in this file
Comments