gon 5.2.2 → 5.2.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gon might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb56c6f655c420fb5f2b19813f08d6ffa9be49eb
4
- data.tar.gz: ac393884fd947158ffa948191d261c2f3f3f3739
3
+ metadata.gz: 43277ce7c2fa158208c211171b2e2e522ed84cfa
4
+ data.tar.gz: 161e43d9aada6fe673fd2f938c856defdcb59810
5
5
  SHA512:
6
- metadata.gz: 0e7693c1b853436ebfa220a91a77ab49d34fad3b9deba29fc8f6c3aa1e4bfcfefa1ba6a2472218462bece617d5cd99c06cabab9c67eafa395dd8c7955276074e
7
- data.tar.gz: 34c336d1e7851168c7f3284f230f10967bbaa3368b900978547e3965ffe143b5d793085b6503f51f13dd821e713b422b7dab646ea833fc9ceb2791e238c1fb88
6
+ metadata.gz: 54085e90ee7e4dbc0a754b6e56b273943fd9f2fb972073a6abd86875a1c067f740f6a79a2e02c133dc437e666181f8adfa54451087a0d5343be96c07ea7a659c
7
+ data.tar.gz: 15c74e24cdfa5e890d6ceb0b408acce28259748ed157d58a1ebcef3bebc1d6ebd21bceec6a93e14c354f9876ffd81abe7721cc6fedd3c12e72305be26a18c2b8
@@ -3,7 +3,7 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.0
6
- - 2.1.1
6
+ - 2.1.4
7
7
  - jruby-19mode # JRuby in 1.9 mode
8
8
  - rbx
9
9
  matrix:
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 5.2.3
4
+
5
+ * Coffescript implementation of watch.js. Thanks to @willcosgrove
6
+ * unwatchAll function in watch.js. Thanks to @willcosgrove
7
+
3
8
  ## 5.2.2
4
9
 
5
10
  * add support for controller helper methods in jbuilder
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
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)
4
4
 
5
5
  [![Build Status](https://secure.travis-ci.org/gazay/gon.png)](http://travis-ci.org/gazay/gon) [![CodeClimate](https://codeclimate.com/github/gazay/gon.png)](https://codeclimate.com/github/gazay/gon)
6
+ [![gon API Documentation](https://www.omniref.com/ruby/gems/gon.png)](https://www.omniref.com/ruby/gems/gon)
6
7
 
7
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!
8
9
 
@@ -0,0 +1,45 @@
1
+ gon._timers = {}
2
+
3
+ gon.watch = (name, possibleOptions, possibleCallback) ->
4
+ return unless $?
5
+
6
+ if typeof possibleOptions == 'object'
7
+ options = {}
8
+ for key, value of gon.watchedVariables[name]
9
+ options[key] = value
10
+ for key, value of possibleOptions
11
+ options[key] = value
12
+ callback = possibleCallback
13
+ else
14
+ options = gon.watchedVariables[name]
15
+ callback = possibleOptions
16
+ performAjax = ->
17
+ xhr = $.ajax
18
+ type: options.type || 'GET'
19
+ url: options.url
20
+ data:
21
+ _method: options.method
22
+ gon_return_variable: true
23
+ gon_watched_variable: name
24
+ xhr.done(callback)
25
+
26
+ if options.interval
27
+ timer = setInterval(performAjax, options.interval)
28
+ gon._timers[name] ?= []
29
+ return gon._timers[name].push
30
+ timer: timer
31
+ fn: callback
32
+ else
33
+ return performAjax()
34
+
35
+ gon.unwatch = (name, fn) ->
36
+ for timer, index in gon._timers[name] when timer.fn == fn
37
+ clearInterval(timer.timer)
38
+ gon._timers[name].splice(index, 1)
39
+ return
40
+
41
+ gon.unwatchAll = ->
42
+ for variable, timers of gon._timers
43
+ for timer in timers
44
+ clearInterval(timer.timer)
45
+ gon._timers = {}
@@ -1,8 +1,9 @@
1
+ // Generated by CoffeeScript 1.7.1
1
2
  gon._timers = {};
2
3
 
3
4
  gon.watch = function(name, possibleOptions, possibleCallback) {
4
- var callback, key, options, performAjax, timer, value, _base, _ref, _ref1;
5
- if (typeof $ === 'undefined' || $ === null) {
5
+ var callback, key, options, performAjax, timer, value, _base, _ref;
6
+ if (typeof $ === "undefined" || $ === null) {
6
7
  return;
7
8
  }
8
9
  if (typeof possibleOptions === 'object') {
@@ -36,7 +37,7 @@ gon.watch = function(name, possibleOptions, possibleCallback) {
36
37
  };
37
38
  if (options.interval) {
38
39
  timer = setInterval(performAjax, options.interval);
39
- if ((_ref1 = (_base = gon._timers)[name]) == null) {
40
+ if ((_base = gon._timers)[name] == null) {
40
41
  _base[name] = [];
41
42
  }
42
43
  return gon._timers[name].push({
@@ -53,10 +54,24 @@ gon.unwatch = function(name, fn) {
53
54
  _ref = gon._timers[name];
54
55
  for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
55
56
  timer = _ref[index];
56
- if (timer.fn === fn) {
57
+ if (!(timer.fn === fn)) {
58
+ continue;
59
+ }
60
+ clearInterval(timer.timer);
61
+ gon._timers[name].splice(index, 1);
62
+ return;
63
+ }
64
+ };
65
+
66
+ gon.unwatchAll = function() {
67
+ var timer, timers, variable, _i, _len, _ref;
68
+ _ref = gon._timers;
69
+ for (variable in _ref) {
70
+ timers = _ref[variable];
71
+ for (_i = 0, _len = timers.length; _i < _len; _i++) {
72
+ timer = timers[_i];
57
73
  clearInterval(timer.timer);
58
- gon._timers[name].splice(index, 1);
59
- return;
60
74
  }
61
75
  }
76
+ return gon._timers = {};
62
77
  };
@@ -1,3 +1,3 @@
1
1
  class Gon
2
- VERSION = '5.2.2'
2
+ VERSION = '5.2.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gon
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.2
4
+ version: 5.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - gazay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-01 00:00:00.000000000 Z
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -150,8 +150,8 @@ files:
150
150
  - Gemfile
151
151
  - LICENSE
152
152
  - README.md
153
- - README_old.md
154
153
  - Rakefile
154
+ - coffee/watch.coffee
155
155
  - doc/logo.png
156
156
  - doc/logo_small.png
157
157
  - doc/top_sample.png
@@ -1,389 +0,0 @@
1
- # Gon gem — get your Rails variables in your js
2
-
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)
4
-
5
-
6
- ### Build Status ![http://travis-ci.org/gazay/gon](https://secure.travis-ci.org/gazay/gon.png)
7
-
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!
9
-
10
- With [Jbuilder](https://github.com/rails/jbuilder) and [Rabl](https://github.com/nesquena/rabl) support!
11
-
12
- For Sinatra available [gon-sinatra](https://github.com/gazay/gon-sinatra).
13
-
14
- For .Net MVC available port [NGon](https://github.com/brooklynDev/NGon).
15
-
16
- ## An example of typical use
17
-
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
-
20
- When you need to send some start data from your controller to your js
21
- you might be doing something like this:
22
-
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
25
- attributes, or write js right in view
26
- 3. Then there can be two ways in js:
27
- + if you previously wrote data in data
28
- attributes - you should parse this attributes and write data to some
29
- js variable.
30
- + if you wrote js right in view (many frontenders would shame you for
31
- that) - you just use data from this js - OK.
32
- 4. You can use your data in your js
33
-
34
- And everytime when you need to send some data from action to js you do this.
35
-
36
- With gon you configure it firstly - just put in layout one tag, and add
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
- ```
50
-
51
- 3. profit?
52
-
53
- ## Usage
54
-
55
- `app/views/layouts/application.html.erb`
56
-
57
- ``` erb
58
- <head>
59
- <title>some title</title>
60
- <%= include_gon %>
61
- <!-- include your action js code -->
62
- ...
63
- ```
64
-
65
- To camelize your variables in js you can use:
66
-
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"
96
-
97
- ``` erb
98
- <head>
99
- <title>some title</title>
100
- <%= include_gon(:need_type => true) %>
101
- <!-- include your action js code with 'serverExports' namespace -->
102
- ...
103
- ```
104
-
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
- ```
114
-
115
- You put something like this in the action of your controller:
116
-
117
- ``` ruby
118
- @your_int = 123
119
- @your_array = [1,2]
120
- @your_hash = {'a' => 1, 'b' => 2}
121
- gon.your_int = @your_int
122
- gon.your_other_int = 345 + gon.your_int
123
- gon.your_array = @your_array
124
- gon.your_array << gon.your_int
125
- gon.your_hash = @your_hash
126
-
127
- gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
128
- gon.your_array # > [1, 2, 123]
129
-
130
- gon.clear # gon.all_variables now is {}
131
- ```
132
-
133
- Access the varaibles from your JavaScript file:
134
-
135
- ``` js
136
- alert(gon.your_int)
137
- alert(gon.your_other_int)
138
- alert(gon.your_array)
139
- alert(gon.your_hash)
140
- ```
141
-
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
-
160
- ## Usage with Rabl
161
-
162
- You can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).
163
- The way of writing Rabl templates is very clearly described in their repo.
164
-
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
-
175
- Profit of using Rabl with gon:
176
-
177
- 1. You can clean your controllers now!
178
- 2. Work with database objects and collections clearly and easyly
179
- 3. All power of Rabl
180
- 4. You can still be lazy and don't use common way to transfer data in js
181
- 5. And so on
182
-
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
- ```
268
-
269
- ## Usage with Jbuilder
270
-
271
- Use gon with [Jbuilder](https://github.com/rails/jbuilder) as with [Rabl](https://guthub.com/nesquena/rabl):
272
-
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.
329
-
330
- ## gon.global
331
-
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
- ```
370
-
371
- Or manually install gon gem: `$ gem install gon`
372
-
373
- ## Contributors
374
-
375
- * @gazay
376
-
377
- Special thanks to @brainopia, @kossnocorp and @ai.
378
-
379
- ## License
380
-
381
- The MIT License
382
-
383
- Copyright (c) 2011-2012 gazay
384
-
385
- 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:
386
-
387
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
388
-
389
- 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.