Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (96 sloc) 5.134 kB
89df60b @gazay Initial commit
authored
1 # Gon gem — get your Rails variables in your js
e133e45 @gazay Typo
authored
2
926ab15 @gazay small logo
authored
3 ![Gon. You should try this. If you look closer - you will see an elephant.](https://github.com/gazay/gon/raw/master/doc/logo_small.png)
72fe424 @gazay An elephant
authored
4
89df60b @gazay Initial commit
authored
5
f6e9940 @gazay Update README.md
authored
6 ### Build Status ![http://travis-ci.org/gazay/gon](https://secure.travis-ci.org/gazay/gon.png)
731657e @gazay Add travis-ci img
authored
7
ed329e5 Typo
Jim Remsik authored
8 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!
89df60b @gazay Initial commit
authored
9
0a349f0 @gazay Added changelog and bumped version to 3.0.0
authored
10 With [Jbuilder](https://github.com/rails/jbuilder) and [Rabl](https://github.com/nesquena/rabl) support!
e133e45 @gazay Typo
authored
11
3104d77 @gazay Link to gon-sinatra
authored
12 For Sinatra available [gon-sinatra](https://github.com/gazay/gon-sinatra).
13
24ea60a @gazay Link for .Net port of Gon
authored
14 For .Net MVC available port [NGon](https://github.com/brooklynDev/NGon).
15
6e5f5a2 @gazay Explanation of usage in JS
authored
16 ## An example of typical use
88e28a3 @gazay Readme and version now 2.0.0
authored
17
22e1210 @gazay Bump version after pull request commits
authored
18 ### Very good and detailed example and reasons to use is considered in [railscast](http://railscasts.com/episodes/324-passing-data-to-javascript) by Ryan Bates
19
88e28a3 @gazay Readme and version now 2.0.0
authored
20 When you need to send some start data from your controller to your js
6e5f5a2 @gazay Explanation of usage in JS
authored
21 you might be doing something like this:
88e28a3 @gazay Readme and version now 2.0.0
authored
22
6e5f5a2 @gazay Explanation of usage in JS
authored
23 1. Write this data in controller(presenter/model) to some variable
24 2. In view for this action you put this variable to some objects by data
edd238f @gazay Readme %#$%
authored
25 attributes, or write js right in view
6e5f5a2 @gazay Explanation of usage in JS
authored
26 3. Then there can be two ways in js:
27 + if you previously wrote data in data
edd238f @gazay Readme %#$%
authored
28 attributes - you should parse this attributes and write data to some
19d5f20 @gazay Bump minor version
authored
29 js variable.
6e5f5a2 @gazay Explanation of usage in JS
authored
30 + if you wrote js right in view (many frontenders would shame you for
edd238f @gazay Readme %#$%
authored
31 that) - you just use data from this js - OK.
32 4. You can use your data in your js
88e28a3 @gazay Readme and version now 2.0.0
authored
33
6e5f5a2 @gazay Explanation of usage in JS
authored
34 And everytime when you need to send some data from action to js you do this.
88e28a3 @gazay Readme and version now 2.0.0
authored
35
36 With gon you configure it firstly - just put in layout one tag, and add
6e5f5a2 @gazay Explanation of usage in JS
authored
37 gem line to your Gemfile and do the following:
38
39 1. Write variables by
40
41 ``` ruby
42 gon.variable_name = variable_value
43 ```
44
45 2. In your js you get this by
46
47 ``` js
48 gon.variable_name
49 ```
88e28a3 @gazay Readme and version now 2.0.0
authored
50
edd238f @gazay Readme %#$%
authored
51 3. profit?
88e28a3 @gazay Readme and version now 2.0.0
authored
52
89df60b @gazay Initial commit
authored
53 ## Usage
54
6be3879 @gazay New readme, all detailed info in wiki now.
authored
55 ### More details about configuration and usage you can find in [gon wiki](https://github.com/gazay/gon/wiki)
020007b @gazay Added description and usage
authored
56
6be3879 @gazay New readme, all detailed info in wiki now.
authored
57 Old readme available in [./README_old.md](https://github.com/gazay/gon/blob/master/README_old.md)
020007b @gazay Added description and usage
authored
58
dd4e7e3 @gazay Now you can use camelized variables in your JS
authored
59
6be3879 @gazay New readme, all detailed info in wiki now.
authored
60 `app/views/layouts/application.html.erb`
9d34903 @torbjon added type text/javascript option
torbjon authored
61
62 ``` erb
63 <head>
64 <title>some title</title>
6be3879 @gazay New readme, all detailed info in wiki now.
authored
65 <%= include_gon %>
66 <!-- include your action js code -->
9d34903 @torbjon added type text/javascript option
torbjon authored
67 ...
68 ```
69
6be3879 @gazay New readme, all detailed info in wiki now.
authored
70 You can pass some [options](https://github.com/gazay/gon/wiki/Options)
71 to `include_gon` method.
725e14d @gazay Added need_tag option. Kudos to @afa
authored
72
c165afc @dnagir minor spelling fixes in README
dnagir authored
73 You put something like this in the action of your controller:
020007b @gazay Added description and usage
authored
74
75 ``` ruby
a4f0807 @gazay Added types to description
authored
76 @your_int = 123
77 @your_array = [1,2]
78 @your_hash = {'a' => 1, 'b' => 2}
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
79 gon.your_int = @your_int
80 gon.your_other_int = 345 + gon.your_int
81 gon.your_array = @your_array
82 gon.your_array << gon.your_int
83 gon.your_hash = @your_hash
7287e85 @gazay Bump version to 1.0.0
authored
84
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
85 gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
86 gon.your_array # > [1, 2, 123]
7287e85 @gazay Bump version to 1.0.0
authored
87
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
88 gon.clear # gon.all_variables now is {}
020007b @gazay Added description and usage
authored
89 ```
90
c165afc @dnagir minor spelling fixes in README
dnagir authored
91 Access the varaibles from your JavaScript file:
020007b @gazay Added description and usage
authored
92
93 ``` js
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
94 alert(gon.your_int)
95 alert(gon.your_other_int)
96 alert(gon.your_array)
97 alert(gon.your_hash)
020007b @gazay Added description and usage
authored
98 ```
89df60b @gazay Initial commit
authored
99
88e28a3 @gazay Readme and version now 2.0.0
authored
100 ## Usage with Rabl
101
0a349f0 @gazay Added changelog and bumped version to 3.0.0
authored
102 You can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).
6e5f5a2 @gazay Explanation of usage in JS
authored
103 The way of writing Rabl templates is very clearly described in their repo.
88e28a3 @gazay Readme and version now 2.0.0
authored
104
105 Profit of using Rabl with gon:
edd238f @gazay Readme %#$%
authored
106
107 1. You can clean your controllers now!
c165afc @dnagir minor spelling fixes in README
dnagir authored
108 2. Work with database objects and collections clearly and easyly
edd238f @gazay Readme %#$%
authored
109 3. All power of Rabl
6e5f5a2 @gazay Explanation of usage in JS
authored
110 4. You can still be lazy and don't use common way to transfer data in js
edd238f @gazay Readme %#$%
authored
111 5. And so on
88e28a3 @gazay Readme and version now 2.0.0
authored
112
6be3879 @gazay New readme, all detailed info in wiki now.
authored
113 [Instruction](https://github.com/gazay/gon/wiki/Usage-with-rabl) for
114 usage gon with Rabl.
88e28a3 @gazay Readme and version now 2.0.0
authored
115
82c6053 @gazay Bump version
authored
116 ## Usage with Jbuilder
117
118 Use gon with [Jbuilder](https://github.com/rails/jbuilder) as with [Rabl](https://guthub.com/nesquena/rabl):
119
6be3879 @gazay New readme, all detailed info in wiki now.
authored
120 [Instruction](https://github.com/gazay/gon/wiki/Usage-with-jbuilder) for
121 usage gon with Jbuilder.
82c6053 @gazay Bump version
authored
122
0a349f0 @gazay Added changelog and bumped version to 3.0.0
authored
123 ## gon.global
124
6be3879 @gazay New readme, all detailed info in wiki now.
authored
125 You can use gon for sending your data to js from anywhere! It's really
126 great for some init data.
89df60b @gazay Initial commit
authored
127
6be3879 @gazay New readme, all detailed info in wiki now.
authored
128 [Instruction](https://github.com/gazay/gon/wiki/Usage-gon-global) for
129 usage gon.global.
89df60b @gazay Initial commit
authored
130
131 ## Contributors
132
133 * @gazay
134
2f1bc0e @gazay Edited README.md via GitHub
authored
135 Special thanks to @brainopia, @kossnocorp and @ai.
89df60b @gazay Initial commit
authored
136
137 ## License
138
139 The MIT License
140
ccc3ecf @gazay Copyright update
authored
141 Copyright (c) 2011-2012 gazay
89df60b @gazay Initial commit
authored
142
143 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
144
145 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
146
147 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.