Skip to content

Commit 6be3879

Browse files
committed
New readme, all detailed info in wiki now.
1 parent ba09265 commit 6be3879

File tree

2 files changed

+404
-257
lines changed

2 files changed

+404
-257
lines changed

README.md

Lines changed: 15 additions & 257 deletions
Original file line numberDiff line numberDiff line change
@@ -52,65 +52,23 @@ gem line to your Gemfile and do the following:
5252

5353
## Usage
5454

55-
`app/views/layouts/application.html.erb`
55+
### More details about configuration and usage you can find in [gon wiki](https://github.com/gazay/gon/wiki)
5656

57-
``` erb
58-
<head>
59-
<title>some title</title>
60-
<%= include_gon %>
61-
<!-- include your action js code -->
62-
...
63-
```
57+
Old readme available in [./README_old.md](https://github.com/gazay/gon/blob/master/README_old.md)
6458

65-
To camelize your variables in js you can use:
6659

67-
``` erb
68-
<head>
69-
<title>some title</title>
70-
<%= include_gon(:camel_case => true) %>
71-
<!-- include your action js code with camelized variables -->
72-
...
73-
```
74-
75-
You can change the namespace of the variables:
76-
77-
``` erb
78-
<head>
79-
<title>some title</title>
80-
<%= include_gon(:namespace => 'serverExports') %>
81-
<!-- include your action js code with 'serverExports' namespace -->
82-
...
83-
```
84-
85-
You can initialize window.gon = {}; on each request
86-
87-
``` erb
88-
<head>
89-
<title>some title</title>
90-
<%= include_gon(:init => true) %>
91-
<!-- include your action js code with 'serverExports' namespace -->
92-
...
93-
```
94-
95-
You can initialize script tag with type="text/javascript"
60+
`app/views/layouts/application.html.erb`
9661

9762
``` erb
9863
<head>
9964
<title>some title</title>
100-
<%= include_gon(:need_type => true) %>
101-
<!-- include your action js code with 'serverExports' namespace -->
65+
<%= include_gon %>
66+
<!-- include your action js code -->
10267
...
10368
```
10469

105-
You can get json without script tag (kudos to @afa):
106-
107-
``` erb
108-
<head>
109-
<title>some title</title>
110-
<script><%= include_gon(:need_tag => false) %></script>
111-
<!-- include your action js code with 'serverExports' namespace -->
112-
...
113-
```
70+
You can pass some [options](https://github.com/gazay/gon/wiki/Options)
71+
to `include_gon` method.
11472

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

@@ -139,39 +97,11 @@ alert(gon.your_array)
13997
alert(gon.your_hash)
14098
```
14199

142-
With camelize:
143-
144-
``` js
145-
alert(gon.yourInt)
146-
alert(gon.yourOtherInt)
147-
alert(gon.yourArray)
148-
alert(gon.yourHash)
149-
```
150-
151-
With custom namespace and camelize:
152-
153-
``` js
154-
alert(customNamespace.yourInt)
155-
alert(customNamespace.yourOtherInt)
156-
alert(customNamespace.yourArray)
157-
alert(customNamespace.yourHash)
158-
```
159-
160100
## Usage with Rabl
161101

162102
You can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).
163103
The way of writing Rabl templates is very clearly described in their repo.
164104

165-
Add Rabl to your Gemfile before requiring gon - because gon checks Rabl constant
166-
167-
`Gemfile`
168-
169-
``` ruby
170-
gem 'rabl'
171-
...
172-
gem 'gon'
173-
```
174-
175105
Profit of using Rabl with gon:
176106

177107
1. You can clean your controllers now!
@@ -180,195 +110,23 @@ Profit of using Rabl with gon:
180110
4. You can still be lazy and don't use common way to transfer data in js
181111
5. And so on
182112
183-
For using gon with Rabl you need to create new Rabl template and map gon
184-
to it.
185-
For example you have model Post with attributes :title and :body.
186-
You want to get all your posts in your js as an Array.
187-
That's what you need to do:
188-
189-
1. Create Rabl template. You can choose spepicific directory but better
190-
use default directory for action.
191-
192-
`app/views/posts/index.json.rabl`
193-
194-
``` rabl
195-
collection @posts => 'posts'
196-
attributes :id, :title, :body
197-
```
198-
199-
2. If you create template in default directory for action, you just write in this action:
200-
201-
`app/controllers/posts_controller.rb#index`
202-
203-
``` ruby
204-
def index
205-
# some controller logic
206-
@posts = Post.all # Rabl works with instance variables of controller
207-
208-
gon.rabl
209-
# some controller logic
210-
end
211-
```
212-
213-
But if you choose some specific category - you need to map this template to gon.
214-
215-
`app/controllers/posts_controller.rb#index`
216-
217-
``` ruby
218-
def index
219-
# some controller logic
220-
@posts = Post.all # Rabl works with instance variables of controller
221-
222-
gon.rabl :template => 'app/goners/posts/index.rabl'
223-
# some controller logic
224-
end
225-
```
226-
227-
Thats it! Now you will get in your js gon.posts variable which is Array of
228-
post objects with attributes :id, :title and :body.
229-
230-
In javascript file for view of this action write call to your variable:
231-
232-
``` js
233-
alert(gon.posts)
234-
alert(gon.posts[0])
235-
alert(gon.posts[0].post.body)
236-
```
237-
238-
P.s. If you didn't put include_gon tag in your html head area - it
239-
wouldn't work. You can read about this in common usage above.
240-
241-
### Some tips of usage Rabl with gon:
242-
243-
If you don't use alias in Rabl template:
244-
245-
``` rabl
246-
collection @posts
247-
....
248-
```
249-
250-
instead of using that:
251-
252-
``` rabl
253-
collection @posts => 'alias'
254-
....
255-
```
256-
257-
Rabl will return you an array and gon by default will put it to variable
258-
gon.rabl
259-
260-
Two ways how you can change it - using aliases or you can add alias to
261-
gon mapping method:
262-
263-
``` ruby
264-
# your controller stuff here
265-
266-
gon.rabl :as => 'alias'
267-
```
113+
[Instruction](https://github.com/gazay/gon/wiki/Usage-with-rabl) for
114+
usage gon with Rabl.
268115
269116
## Usage with Jbuilder
270117
271118
Use gon with [Jbuilder](https://github.com/rails/jbuilder) as with [Rabl](https://guthub.com/nesquena/rabl):
272119
273-
0. Add jbuilder to your Gemfile (because of it depends on
274-
ActiveSuppurt '~> 3.0.0')
275-
276-
`Gemfile`
277-
278-
``` ruby
279-
gem 'jbuilder'
280-
```
281-
282-
1. Create Jbuilder template.
283-
284-
`app/views/posts/index.json.jbuilder`
285-
286-
``` jbuilder
287-
json.posts @posts, :id, :title, :body
288-
```
289-
290-
2. In your controller you should just call 'gon.jbuilder' - if your template in
291-
default directory for action. In the other case - you still can use :template option.
292-
293-
``` ruby
294-
def index
295-
# some controller logic
296-
@posts = Post.all
297-
298-
gon.jbuilder
299-
# some controller logic
300-
end
301-
```
302-
303-
In javascript file for view of this action write call to your variable:
304-
305-
Now you can use partials in jbuilder:
306-
307-
`app/views/posts/index.json.jbuilder`
308-
309-
``` jbuilder
310-
json.partial! 'app/views/posts/_part.json.jbuilder', :comments => @posts[0].comments
311-
```
312-
313-
`app/views/posts/_part.json.jbuilder`
314-
315-
``` jbuilder
316-
json.comments comments.map{ |it| 'comment#' + it.id }
317-
```
318-
319-
``` js
320-
alert(gon.posts)
321-
alert(gon.posts[0])
322-
alert(gon.posts[0].post.body)
323-
alert(gon.comments)
324-
alert(gon.comments[0])
325-
```
326-
327-
P.s. If you didn't put include_gon tag in your html head area - it
328-
wouldn't work. You can read about this in common usage above.
120+
[Instruction](https://github.com/gazay/gon/wiki/Usage-with-jbuilder) for
121+
usage gon with Jbuilder.
329122
330123
## gon.global
331124
332-
Now you can use gon for sending your data to js from anywhere!
333-
334-
It works just as simple `gon` but you need to write `Gon.global` instead of `gon` in your ruby code,
335-
`gon.global` in javascript and it will not clear self after each request. All other things remain the same.
336-
337-
For example I want to set start data into gon, which will be there before I clear it.
338-
339-
Maybe some configuration data or url address which should be present on each page with `include_gon` helper in head.
340-
341-
Now with Gon.global it's easy!
342-
343-
`config/initializers/some_initializer.rb or any file where you can reach Gon constant`
344-
345-
```ruby
346-
Gon.global.variable = 'Some data'
347-
```
348-
349-
`in some js which can reach window.gon variable`
350-
351-
```javascript
352-
alert(gon.global.variable)
353-
```
354-
355-
Thats it!
356-
357-
## Installation
358-
359-
Puts this line into `Gemfile` then run `$ bundle`:
360-
361-
``` ruby
362-
gem 'gon', '3.0.5'
363-
```
364-
365-
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
366-
367-
``` ruby
368-
config.gem 'gon', :version => '3.0.5'
369-
```
125+
You can use gon for sending your data to js from anywhere! It's really
126+
great for some init data.
370127

371-
Or manually install gon gem: `$ gem install gon`
128+
[Instruction](https://github.com/gazay/gon/wiki/Usage-gon-global) for
129+
usage gon.global.
372130

373131
## Contributors
374132

0 commit comments

Comments
 (0)