Why? ¶
The need for such an extensions came while working on web apps with mixed approach: rich JavaScript GUI on web page, but each page is reloaded and most of bussiness logic is still on the backend side.
The simplest solution for a quick prototyping is pushing a data from PHP controller to global JS variable. That makes you free from a lot of REST API getters (you can make a lot without an API at all).
Install ¶
Via Composer
$ composer require ijackua/gon
Configure ¶
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,
)
), ~~~
Usage ¶
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"
* [Announce and details](http://stdout.in/en/post/yii2-component-yii2gon-push-variables-from-php-to-js)
* [GitHub](https://github.com/iJackUA/yii2-gon)
* [Packagist](https://packagist.org/packages/ijackua/yii2-gon)
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.