Skip to content

Commit 6e5f5a2

Browse files
committed
Explanation of usage in JS
1 parent edd238f commit 6e5f5a2

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

README.md

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,39 @@ If you need to send some data to your js files and you don't want to do this wit
55

66
Now with [Rabl](https://github.com/nesquena/rabl) support!
77

8-
## A example of typical use
8+
## An example of typical use
99

1010
When you need to send some start data from your controller to your js
11-
you probably doing something like this:
11+
you might be doing something like this:
1212

13-
1. Write this data in controller(presenter/model) in some variable
14-
2. In view for this action you put this data in some objects by data
13+
1. Write this data in controller(presenter/model) to some variable
14+
2. In view for this action you put this variable to some objects by data
1515
attributes, or write js right in view
16-
3. Then in js it depends - if you previously write data in data
16+
3. Then there can be two ways in js:
17+
+ if you previously wrote data in data
1718
attributes - you should parse this attributes and write data to some
18-
js variable. If you write js right in view (many frontenders shame you for
19+
js variable.
20+
+ if you wrote js right in view (many frontenders would shame you for
1921
that) - you just use data from this js - OK.
2022
4. You can use your data in your js
2123

22-
And everytime when you need some data from action to js you do this.
24+
And everytime when you need to send some data from action to js you do this.
2325

2426
With gon you configure it firstly - just put in layout one tag, and add
25-
gem line to your Gemfile and do two actions:
27+
gem line to your Gemfile and do the following:
28+
29+
1. Write variables by
30+
31+
``` ruby
32+
gon.variable_name = variable_value
33+
```
34+
35+
2. In your js you get this by
36+
37+
``` js
38+
gon.variable_name
39+
```
2640

27-
1. Write variables by gon.variable_name = variable_value
28-
2. In your js you get this by gon.variable_name
2941
3. profit?
3042

3143
## Usage
@@ -107,24 +119,24 @@ alert(customNamespace.yourHash)
107119

108120
## Usage with Rabl
109121

110-
Now you can write your variables assign logic in templates with [Rabl](https://github.com/nesquena/rabl).
111-
How write templates very clearly described in their repo.
122+
Now you can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).
123+
The way of writing Rabl templates is very clearly described in their repo.
112124

113125
Profit of using Rabl with gon:
114126

115127
1. You can clean your controllers now!
116128
2. Clear and easy work with database objects and collections
117129
3. All power of Rabl
118-
4. You still can be lazy and don't use common way to transfer data in js
130+
4. You can still be lazy and don't use common way to transfer data in js
119131
5. And so on
120132
121133
For using gon with Rabl you need to create new Rabl template and map gon
122134
to it.
123135
For example you have model Post with attributes :title and :body.
124136
You want to get all your posts in your js as an Array.
125-
Thats what you need to do:
137+
That's what you need to do:
126138

127-
1. Create Rabl template. I preffer creating special directory for
139+
1. Create Rabl template. I prefer creating special directory for
128140
templates which are not view templates.
129141

130142
`app/goners/posts/index.rabl`
@@ -134,7 +146,7 @@ Thats what you need to do:
134146
attributes :id, :title, :body
135147
```
136148

137-
2. After that you need only map this template to gon.
149+
2. All you need to do after that is only to map this template to gon.
138150

139151
`app/controllers/post_controller.rb#index`
140152

@@ -144,12 +156,21 @@ Thats what you need to do:
144156
@posts = Post.all # Rabl works with instance variables of controller
145157
146158
gon.rabl 'app/goners/posts/index.rabl'
159+
# some controller logic
147160
end
148161
```
149162

150-
Thats it! In your js now you get gon.posts variable which is Array of
163+
Thats it! Now you will get in your js gon.posts variable which is Array of
151164
post objects with attributes :id, :title and :body.
152165

166+
In javascript file for view of this action write call to your variable:
167+
168+
``` js
169+
alert(gon.posts)
170+
alert(gon.posts[0])
171+
alert(gon.posts[0].post.body)
172+
```
173+
153174
P.s. If you didn't put include_gon tag in your html head area - it
154175
wouldn't work. You can read about this in common usage above.
155176

@@ -169,10 +190,10 @@ collection @posts => 'alias'
169190
....
170191
```
171192
172-
Rabl will return you array and gon by default put it to variable
193+
Rabl will return you an array and gon by default will put it to variable
173194
gon.rabl
174195
175-
Two way how you can change it - using aliases or you can add alias to
196+
Two ways how you can change it - using aliases or you can add alias to
176197
gon mapping method:
177198
178199
``` ruby

0 commit comments

Comments
 (0)