reins 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -22
- data/lib/reins/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1af0eeaf611a0c430bb05eef27b354f126a50b9
|
4
|
+
data.tar.gz: 648ccd2171cbf03f3a44db40647c3f3b35440000
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52fa48f4129ee24a7ea6d40eb72f56bdab5462f65c463f1bf833a7f7e31d0ea5b71da053e4dddf42a59c094627a29e9d2c0abcddd86ca945979d06466db2e1ac
|
7
|
+
data.tar.gz: '0681b9869d9b7b0931a22bff3a10e3332e5dd078c43d2238a9f6acae7e144bffd424ddda785a4af797dda366ee4b73d1aef50ea9a7e183f44be5c40d5f72c4e7'
|
data/README.md
CHANGED
@@ -8,39 +8,39 @@
|
|
8
8
|
[![Gem Version](https://badge.fury.io/rb/reins.svg)](http://badge.fury.io/rb/reins)
|
9
9
|
![](http://ruby-gem-downloads-badge.herokuapp.com/reins)
|
10
10
|
|
11
|
+
### TL;DR
|
12
|
+
Reins provides an easy way to have separate jQuery ```$(document).ready``` and turbolinks ```page:load``` handlers for each of your controllers and actions. This will avoid weird initialization issues with conflicting selectors, and will help you know where JS initialization code for each page lives.
|
13
|
+
|
11
14
|
### Why reins?
|
12
|
-
Rails' convention over configuration approach to development is well known, however when it comes to javascript it is sorely
|
13
|
-
|
14
|
-
Since all the code is minified and aggregated into application.js by the assets pipeline, it's very easy to end up with a big mess.
|
15
|
+
Rails' convention over configuration approach to development is well known, however when it comes to javascript it is sorely lacking - every time you create a scaffold, resource, or controller, you get a new javascript file named after that controller, but that's it.
|
16
|
+
Then all your JS code is minified and aggregated into application.js by the assets pipeline with multiple ```$.ready``` callbacks that run on every page.
|
15
17
|
|
16
|
-
This means that you need to manually find a way to keep the global scope clean, avoid name collisions and define a common structure for your javascript code.
|
18
|
+
This means that you need to manually find a way to keep the global scope clean, avoid name collisions and define a common structure for your javascript code. If you're not very careful with your code's structure it's very easy to end up with a big unmaintainable mess as your application grows.
|
17
19
|
|
18
20
|
|
19
|
-
Reins is a ruby gem that automates the creation of
|
20
|
-
It creates a predefined structure for your javascript code where code for different controllers is kept in separate namespaces.
|
21
|
-
It also enables you to keep javascript code out of your views by defining functions that get called automatically on jQuery.ready and Turbolinks page:load for each rails controller action that runs.
|
21
|
+
Reins is a ruby gem that automates the creation of jQuery ```$.ready``` and turbolinks ```page:load``` callbacks matching the structure of your rails controllers. It also creates a predefined structure for your javascript that helps you keep code for different controllers in separate namespaces.
|
22
22
|
|
23
|
-
|
24
|
-
You just have to define a special hash on the controller and this hash will be JSONified and passed to the reins javascript controller as a dictionary object.
|
23
|
+
Reins even works with javascript code that needs data from the server (which almost always ends up inside a script tag somewhere with erb thrown in the mix to include server-side data...).
|
24
|
+
You just have to define a special hash on the controller and this hash will be JSONified and passed to the reins javascript controller as a dictionary object. We'll take a look at this further down.
|
25
25
|
|
26
26
|
### How can I start using it?
|
27
27
|
|
28
28
|
Add reins to your Gemfile:
|
29
29
|
|
30
|
-
```
|
30
|
+
```ruby
|
31
31
|
gem 'reins'
|
32
32
|
```
|
33
33
|
|
34
34
|
Add reins.js to application.js:
|
35
35
|
|
36
|
-
```
|
36
|
+
```javascript
|
37
37
|
//= require reins
|
38
38
|
```
|
39
39
|
|
40
40
|
Add the reins script tag to your application layout, or any layout/view where you want reins to automatically call your javascript controllers.
|
41
41
|
I prefer just adding it to ```layouts/application.html.erb``` (and any other layouts I might have) right before closing the ```<body>``` tag:
|
42
42
|
|
43
|
-
```
|
43
|
+
```html
|
44
44
|
...
|
45
45
|
<body>
|
46
46
|
...
|
@@ -59,7 +59,7 @@ If the above steps went ok, now is the perfect time for an example:
|
|
59
59
|
|
60
60
|
Suppose we generate a HelloController and add the following code inside:
|
61
61
|
|
62
|
-
```
|
62
|
+
```ruby
|
63
63
|
class HelloController < ApplicationController
|
64
64
|
|
65
65
|
def index
|
@@ -73,15 +73,15 @@ end
|
|
73
73
|
|
74
74
|
Let's quickly create a controller and view by running:
|
75
75
|
```
|
76
|
-
rails g controller hello
|
77
|
-
touch app/views/hello/index.html.erb
|
76
|
+
$ rails g controller hello
|
77
|
+
$ touch app/views/hello/index.html.erb
|
78
78
|
```
|
79
79
|
Don't worry about the empty view for now, as it is irrelevant for reins to work :)
|
80
80
|
When you run the rails generator, reins will create a javascript file at
|
81
81
|
/app/assets/javascripts/controllers/cookies.js with some boilerplate code. After
|
82
82
|
changing a few lines it might look like this:
|
83
83
|
|
84
|
-
```
|
84
|
+
```javascript
|
85
85
|
/**
|
86
86
|
* Anonymous self-running closure to avoid polluting the global namespace with
|
87
87
|
* definitions that should be kept private.
|
@@ -158,9 +158,8 @@ Most of the functionality is explained in the comments inside the javascript sni
|
|
158
158
|
|
159
159
|
#### What if I'm rendering a js.erb view?
|
160
160
|
|
161
|
-
Reins will not be called automatically for these, as the whole idea of js.erb views is to give you
|
162
|
-
However, to avoid repeating code, you can call your reins controller from js.erb views.
|
163
|
-
You just need to do this:
|
161
|
+
Reins will not be called automatically for these, as the whole idea of js.erb views is to give you fine tuned control.
|
162
|
+
However, to avoid repeating code, you can call your reins controller from js.erb views if you want. You just need to do this:
|
164
163
|
```
|
165
164
|
<%= call_reins_controller %>
|
166
165
|
```
|
@@ -168,14 +167,15 @@ This will call the matching controller and pass the @reins_params hash if it was
|
|
168
167
|
|
169
168
|
#### Why do my controllers only get called on page:load when using turbolinks? I want page:change!
|
170
169
|
|
170
|
+
When using turbolinks, ```page:change``` can mean that we are rendering a page from the turbolinks cache with no server intervention at all. You shoud realize that calling the reins controller from this event will use the last params that were sent by the server, so it's probably not what you want.
|
171
|
+
To reiterate, this could mean that if you are going back to a page that was cached by turbolinks, the controller could be called with params from another controller/action, which is probably not what you want.
|
172
|
+
|
171
173
|
Just add this to your application.js:
|
172
|
-
```
|
174
|
+
```javascript
|
173
175
|
$(document).off('page:load').on('page:change', r.call_js_controller);
|
174
176
|
```
|
175
177
|
THIS WILL REMOVE any page:load event handlers that you may have attached to $(document), so be warned!
|
176
178
|
|
177
|
-
Also of note is that calling the reins controller in this way will use the last params that were sent by the server.
|
178
|
-
This means that if you are going back to a page that was cached by turbolinks, the controller could be called with params from another controller/action, which is probably not what you want.
|
179
179
|
|
180
180
|
|
181
181
|
|
data/lib/reins/version.rb
CHANGED
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reins
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Goncalves
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
requirements: []
|
94
94
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
95
|
+
rubygems_version: 2.6.8
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Reins! Rein in your javascript
|