Yii 2.0: yii2-gon

This component publishes json serializable data as 'key -> value' from PHP code into global JS variable `window.gon`.
1 follower

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"

Be the first person to leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet