Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 214 lines (152 sloc) 6.046 kb
89df60b @gazay Initial commit
authored
1 # Gon gem — get your Rails variables in your js
88e28a3 @gazay Readme and version now 2.0.0
authored
2 ## Now with [Rabl](https://github.com/nesquena/rabl) support!
89df60b @gazay Initial commit
authored
3
ed329e5 Typo
Jim Remsik authored
4 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
5
88e28a3 @gazay Readme and version now 2.0.0
authored
6 ## A example of typical use
7
8 When you need to send some start data from your controller to your js
9 you probably doing something like this:
10
11 1. Write this data in controller(presenter/model) in some variable
12 2. In view for this action you put this data in some objects by data
13 attributes, or write js right in view
14 3. Then in js it depends - if you previously write data in data
15 attributes - you should parse this attributes and write data to some
16 js variable. If you write js right in view (many frontenders shame you for
17 that) - you just use data from this js - OK.
18 4. You can use your data in your js
19
20 And everytime when you need some data from action to js you do this.
21
22 With gon you configure it firstly - just put in layout one tag, and add
23 gem line to your Gemfile and do two actions:
24
25 1. Write variables by gon.variable_name = variable_value
26 2. In your js you get this by gon.variable_name
27 3. profit?
28
89df60b @gazay Initial commit
authored
29 ## Usage
30
020007b @gazay Added description and usage
authored
31 `app/views/layouts/application.html.erb`
32
33 ``` erb
34 <head>
35 <title>some title</title>
83b22e3 @gazay Js generation
authored
36 <%= include_gon %>
37 <!-- include your action js code -->
020007b @gazay Added description and usage
authored
38 ...
39 ```
40
dd4e7e3 @gazay Now you can use camelized variables in your JS
authored
41 For camelize your variables in js you can use:
42
43 ``` erb
44 <head>
45 <title>some title</title>
46 <%= include_gon(:camel_case => true) %>
47 <!-- include your action js code with camelized variables -->
48 ...
49 ```
50
d55978f @gazay Added custom namespacing for js gon
authored
51 For different namespace of your variables in js you can use:
52
53 ``` erb
54 <head>
55 <title>some title</title>
56 <%= include_gon(:namespace => 'serverExports') %>
57 <!-- include your action js code with 'serverExports' namespace -->
58 ...
59 ```
60
020007b @gazay Added description and usage
authored
61 In action of your controller you put something like this:
62
63 ``` ruby
a4f0807 @gazay Added types to description
authored
64 @your_int = 123
65 @your_array = [1,2]
66 @your_hash = {'a' => 1, 'b' => 2}
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
67 gon.your_int = @your_int
68 gon.your_other_int = 345 + gon.your_int
69 gon.your_array = @your_array
70 gon.your_array << gon.your_int
71 gon.your_hash = @your_hash
7287e85 @gazay Bump version to 1.0.0
authored
72
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
73 gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
74 gon.your_array # > [1, 2, 123]
7287e85 @gazay Bump version to 1.0.0
authored
75
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
76 gon.clear # gon.all_variables now is {}
020007b @gazay Added description and usage
authored
77 ```
78
79 In javascript file for view of this action write call to your variable:
80
81 ``` js
9feb32b @gazay Bump version to 1.1.0 - this... is... STABLE!!!
authored
82 alert(gon.your_int)
83 alert(gon.your_other_int)
84 alert(gon.your_array)
85 alert(gon.your_hash)
020007b @gazay Added description and usage
authored
86 ```
89df60b @gazay Initial commit
authored
87
dd4e7e3 @gazay Now you can use camelized variables in your JS
authored
88 With camelize:
89
90 ``` js
91 alert(gon.yourInt)
92 alert(gon.yourOtherInt)
93 alert(gon.yourArray)
94 alert(gon.yourHash)
95 ```
96
d55978f @gazay Added custom namespacing for js gon
authored
97 With custom namespace and camelize:
98
99 ``` js
100 alert(customNamespace.yourInt)
101 alert(customNamespace.yourOtherInt)
102 alert(customNamespace.yourArray)
103 alert(customNamespace.yourHash)
104 ```
105
88e28a3 @gazay Readme and version now 2.0.0
authored
106 ## Usage with Rabl
107
108 Now you can write your variables assign logic in templates with [Rabl](https://github.com/nesquena/rabl).
109 How write templates very clearly described in their repo.
110
111 Profit of using Rabl with gon:
112 1. You can clean your controllers now!
113 2. Clear and easy work with database objects and collections
114 3. All power of Rabl
115 4. You still can be lazy and don't use common way to transfer data in js
116 5. And so on
117
118 For using gon with Rabl you need to create new Rabl template and map gon
119 to it.
120 For example you have model Post with attributes :title and :body.
121 You want to get all your posts in your js as an Array.
122 Thats what you need to do:
123
124 1. Create Rabl template. I preffer creating special directory for
125 templates which are not view templates.
126
127 `app/goners/posts/index.rabl`
128
129 ``` rabl
130 collection @posts => 'posts'
131 attributes :id, :title, :body
132 ```
133
134 2. After that you need only map this template to gon.
135
136 `app/controllers/post_controller.rb#index`
137
138 ``` ruby
139 def index
140 # some controller logic
141 @posts = Post.all # Rabl works with instance variables of controller
142
143 gon.rabl 'app/goners/posts/index.rabl'
144 end
145 ```
146
147 Thats it! In your js now you get gon.posts variable which is Array of
148 post objects with attributes :id, :title and :body.
149
150 P.s. If you didn't put include_gon tag in your html head area - it
151 wouldn't work. You can read about this in common usage above.
152
153 ### Some tips of usage Rabl with gon:
154
155 If you don't use alias in Rabl template:
156
157 ``` rabl
158 collection @posts
159 ....
160 ```
161
162 instead of using that:
163
164 ``` rabl
165 collection @posts => 'alias'
166 ....
167 ```
168
169 Rabl will return you array and gon by default put it to variable
170 gon.rabl
171
172 Two way how you can change it - using aliases or you can add alias to
173 gon mapping method:
174
175 ``` ruby
176 # your controller stuff here
177
178 gon.rabl 'path/to/rabl/file', :as => 'alias'
179 ```
180
89df60b @gazay Initial commit
authored
181 ## Installation
182
183 Puts this line into `Gemfile` then run `$ bundle`:
184
185 ``` ruby
88e28a3 @gazay Readme and version now 2.0.0
authored
186 gem 'gon', '2.0.0'
89df60b @gazay Initial commit
authored
187 ```
188
189 Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
190
191 ``` ruby
88e28a3 @gazay Readme and version now 2.0.0
authored
192 config.gem 'gon', :version => '2.0.0'
89df60b @gazay Initial commit
authored
193 ```
194
da310ad @gazay Edited README.md via GitHub
authored
195 Or manually install gon gem: `$ gem install gon`
89df60b @gazay Initial commit
authored
196
197 ## Contributors
198
199 * @gazay
200
2f1bc0e @gazay Edited README.md via GitHub
authored
201 Special thanks to @brainopia, @kossnocorp and @ai.
89df60b @gazay Initial commit
authored
202
203 ## License
204
205 The MIT License
206
207 Copyright (c) 2011 gazay
208
209 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:
210
211 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
212
213 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.