• Stars
    star
    3,036
  • Rank 14,856 (Top 0.3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Your Rails variables in your JS

Gon gem β€” get your Rails variables in your js

Join the chat at https://gitter.im/gazay/gon

Gon. You should try this. If you look closer - you will see an elephant.

Build Status CodeClimate

If you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force!

Now you can easily renew data in your variables through ajax with gon.watch!

With Jbuilder, Rabl, and Rabl-Rails support!

For Sinatra available gon-sinatra.

For .Net MVC available port NGon.

For elixir Phoenix available PhoenixGon.

Sponsored by Evil Martians

An example of typical use

Very good and detailed example and reasons to use is considered in railscast by Ryan Bates

When you need to send some start data from your controller to your js you might be doing something like this:

  1. Write this data in controller(presenter/model) to some variable
  2. In view for this action you put this variable to some objects by data attributes, or write js right in view
  3. Then there can be two ways in js: + if you previously wrote data in data attributes - you should parse this attributes and write data to some js variable. + if you wrote js right in view (many frontenders would shame you for that) - you just use data from this js - OK.
  4. You can use your data in your js

And every time when you need to send some data from action to js you do this.

With gon you configure it firstly - just put in layout one tag, and add gem line to your Gemfile and do the following:

  1. Write variables by
 gon.variable_name = variable_value

 # or new syntax
 gon.push({
   :user_id => 1,
   :user_role => "admin"
 })

 gon.push(any_object) # any_object with respond_to? :each_pair
  1. In your js you get this by
 gon.variable_name
  1. profit?

With the gon.watch feature you can easily renew data in gon variables! Simply call gon.watch from your js file. It's super useful in modern web applications!

Usage

More details about configuration and usage you can find in gon wiki

app/views/layouts/application.html.erb

<head>
  <title>some title</title>
  <%= Gon::Base.render_data %>
  <!-- include your action js code -->
  ...

For rails 3:

  <%= include_gon %>
  ...

You can pass some options to render_data method.

You put something like this in the action of your controller:

@your_int = 123
@your_array = [1,2]
@your_hash = {'a' => 1, 'b' => 2}
gon.your_int = @your_int
gon.your_other_int = 345 + gon.your_int
gon.your_array = @your_array
gon.your_array << gon.your_int
gon.your_hash = @your_hash

gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
gon.your_array # > [1, 2, 123]

# gon.clear # gon.all_variables now is {}

Access the variables from your JavaScript file:

alert(gon.your_int)
alert(gon.your_other_int)
alert(gon.your_array)
alert(gon.your_hash)

AMD compatible version: include_gon_amd

If your site uses AMD modules you can use the include_gon_amd helper to include the variables and watch function as a module. Options are mostly the same as for include_gon, except for namespace_check, which does nothing and namespace, which is used as the name of the defined module. The end result will look somewhat like the following:

define('yourNameSpace', [], function() {
  var gon = {};
  gon.yourVariable = yourValue;
  // etc...

  return gon;
});

A (very) simplified usage example:

app/views/layouts/application.html.erb

include_gon_amd namespace: 'data'

Some JavaScript module

define(['data'], function(data) {
  alert(data.myVariable);
});

gon.watch - renew your data easily!

You can use gon for renewing your data without reloading pages and writing long js functions! It's really great for some live values.

Supports gon.watch.rabl and gon.watch.jbuilder usage.

Instruction for usage gon.watch.

Usage with Rabl

You can write your variables assign logic to templates with Rabl. The way of writing Rabl templates is very clearly described in their repo.

Profit of using Rabl with gon:

  1. You can clean your controllers now!
  2. Work with database objects and collections clearly and easyly
  3. All power of Rabl
  4. You can still be lazy and don't use common way to transfer data in js
  5. And so on

Instruction for usage gon with Rabl.

Usage with Rabl-Rails

gon.rabl works with rabl-rails. Learn to write RABL the rabl-rails way here.

Add gon and rabl-rails to your environment:

gem 'gon'
gem 'rabl-rails'

Define a rabl template using rabl-rails syntax:

#app/views/users/show.rabl
object :@user
attributes :id, :name, :email, :location

Call gon.rabl in your controller

#app/controllers/users_controller.rb
def show
  @user = User.find(params[:id])
  gon.rabl
end

Usage with Jbuilder

Use gon with Jbuilder as with Rabl:

Instruction for usage gon with Jbuilder.

gon.global

You can use gon for sending your data to js from anywhere! It's really great for some init data.

Instruction for usage gon.global.

Speed up Gon

You can use any JSON Engine you want. Gon uses MultiJson with autodetect mode, so all you need is just require your JSON library.

Contributors

  • @gazay
  • @takiy33

Special thanks to @brainopia, @kossnocorp and @ai.

License

The MIT License

Security Contact

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

More Repositories

1

ids_please

Helps to get IDs or screen names from links to social network accounts
Ruby
155
star
2

talks

Talking hooks for long lasted cli tools
Ruby
103
star
3

gon-sinatra

Your Sinatra variables in your JS
Ruby
83
star
4

node-emoji-clock

JavaScript
78
star
5

share_some_love

Share some love to our buddies rubyists!
Ruby
26
star
6

odnoklassniki

Ruby wrapper for Odnoklassniki API
Ruby
24
star
7

tonic

gh-pages with sass, haml, coffee (and compass)
Ruby
23
star
8

environment

Ubuntu install and setup
Shell
12
star
9

spik

Let spik with Rails!
Ruby
11
star
10

mnemonic

Simple logger to show where is leaking by many-many puts
Ruby
10
star
11

is

Small simple tool for finding points in polygons
Ruby
6
star
12

gobrain

Rails Rumble Entry for Gobrain
JavaScript
4
star
13

repka

Repka will help you easily gather stats from controllers if you use mongoid
Ruby
4
star
14

dlnd-project-02

Image classification problem with convolutional networks
Jupyter Notebook
3
star
15

diffit

A simple solution to track changes in your tables.
Ruby
2
star
16

dlnd_tv_script_generation

HTML
2
star
17

movie-alert

Repository for the RailsRumble 2009 Team 74
JavaScript
2
star
18

node-utf-format

β’»β“žβ“‘β“œβ“β“£ ᴛᴇxα΄› with utf-8 power
JavaScript
2
star
19

kubik

Sugar for kubernetes for lazy developers
2
star
20

lysa-old

Clone of https://github.com/learnyou/lysa
TeX
2
star
21

postcss-demo

Gulp application to check how all PostCSS plugins work
HTML
2
star
22

hat_on_the_head

Jupyter Notebook
1
star
23

anybitchcancode

really
1
star
24

recsys_learning

Jupyter Notebook
1
star
25

scheme48

http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours
1
star
26

bioreactor

Repository for the RailsRumble 2010 Team 69
JavaScript
1
star
27

repka_ar

Easy stats collecting with ActiveRecord (Use REPKA and MongoDB, Luke). This is PRE-ALPHA, when this will work, i'll let you know ;)
Ruby
1
star
28

friendly_admin

Friendly admin
Ruby
1
star
29

nuclear_safety

diploma project to solving nuclear safety problem
Ruby
1
star
30

dlnd-project-01

First project of dlnd
HTML
1
star
31

battletoads

Tools to generate demo applications for PostCSS plugins
Ruby
1
star
32

ruby-lang.org-templates

Translation templates for www.ruby-lang.org news
1
star
33

dlnd-project-04

Language translation project from dlnd
HTML
1
star
34

dlnd-face-generation

final project of DLND
HTML
1
star