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.
When you need to send some start data from your controller to your js you might be doing something like this:
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:
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
In your js you get this by
gon.variable_name
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!
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)
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);
});
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.
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:
Instruction for usage gon with Rabl.
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
Use gon with Jbuilder as with Rabl:
Instruction for usage gon with Jbuilder.
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.
You can use any JSON Engine you want.
Gon uses MultiJson
with autodetect mode, so all you need is just require your JSON library.
Special thanks to @brainopia, @kossnocorp and @ai.
The MIT License
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.