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

  1. Why?
  2. Install
  3. Configure
  4. Usage

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)

0 0
1 follower
0 downloads
Yii Version: 2.0
License: MIT
Category: User Interface
Developed by: iJackUA
Created on: Dec 6, 2015
Last updated: 9 years ago

Related Extensions