paloma 2.0.6 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +3 -75
- data/README.md +89 -376
- data/TODO.md +1 -13
- data/app/views/paloma/_hook.html.erb +31 -0
- data/lib/paloma.rb +1 -25
- data/lib/paloma/action_controller_extension.rb +72 -38
- data/paloma.gemspec +7 -7
- data/{spec/test_app → test_app}/.gitignore +0 -0
- data/{spec/test_app → test_app}/.rspec +0 -0
- data/{spec/test_app → test_app}/Gemfile +1 -2
- data/{spec/test_app → test_app}/README.rdoc +0 -0
- data/{spec/test_app → test_app}/Rakefile +2 -7
- data/test_app/app/assets/images/rails.png +0 -0
- data/test_app/app/assets/javascripts/admin/bar.js +2 -0
- data/test_app/app/assets/javascripts/application.js +83 -0
- data/test_app/app/assets/javascripts/foo.js +2 -0
- data/test_app/app/assets/javascripts/main.js +2 -0
- data/test_app/app/assets/stylesheets/admin/bar.css +4 -0
- data/test_app/app/assets/stylesheets/application.css +13 -0
- data/test_app/app/assets/stylesheets/foo.css +4 -0
- data/test_app/app/assets/stylesheets/main.css +4 -0
- data/test_app/app/controllers/admin/bar_controller.rb +7 -0
- data/{spec/test_app → test_app}/app/controllers/application_controller.rb +0 -0
- data/test_app/app/controllers/foo_controller.rb +19 -0
- data/test_app/app/controllers/main_controller.rb +7 -0
- data/test_app/app/helpers/admin/bar_helper.rb +2 -0
- data/test_app/app/helpers/application_helper.rb +2 -0
- data/test_app/app/helpers/foo_helper.rb +2 -0
- data/test_app/app/helpers/main_helper.rb +2 -0
- data/{spec/test_app/log → test_app/app/mailers}/.gitkeep +0 -0
- data/{spec/test_app/public/favicon.ico → test_app/app/models/.gitkeep} +0 -0
- data/test_app/app/views/layouts/application.html.erb +14 -0
- data/{spec/test_app → test_app}/config.ru +0 -0
- data/{spec/test_app → test_app}/config/application.rb +2 -7
- data/{spec/test_app → test_app}/config/boot.rb +0 -0
- data/{spec/test_app → test_app}/config/environment.rb +0 -0
- data/{spec/test_app → test_app}/config/environments/development.rb +6 -0
- data/{spec/test_app → test_app}/config/environments/production.rb +3 -0
- data/{spec/test_app → test_app}/config/environments/test.rb +2 -0
- data/{spec/test_app → test_app}/config/initializers/backtrace_silencers.rb +0 -0
- data/{spec/test_app → test_app}/config/initializers/inflections.rb +0 -0
- data/{spec/test_app → test_app}/config/initializers/mime_types.rb +0 -0
- data/{spec/test_app → test_app}/config/initializers/secret_token.rb +1 -1
- data/{spec/test_app → test_app}/config/initializers/session_store.rb +0 -0
- data/{spec/test_app → test_app}/config/initializers/wrap_parameters.rb +4 -0
- data/{spec/test_app → test_app}/config/locales/en.yml +0 -0
- data/test_app/config/routes.rb +14 -0
- data/test_app/db/seeds.rb +7 -0
- data/test_app/doc/README_FOR_APP +2 -0
- data/test_app/lib/assets/.gitkeep +0 -0
- data/test_app/lib/tasks/.gitkeep +0 -0
- data/test_app/log/.gitkeep +0 -0
- data/{spec/test_app → test_app}/public/404.html +0 -0
- data/{spec/test_app → test_app}/public/422.html +0 -0
- data/{spec/test_app → test_app}/public/500.html +0 -0
- data/test_app/public/favicon.ico +0 -0
- data/{spec/test_app → test_app}/public/robots.txt +0 -0
- data/{spec/test_app → test_app}/script/rails +0 -0
- data/test_app/spec/integration/main_spec.rb +61 -0
- data/test_app/spec/javascripts/controller_builder_spec.js +48 -0
- data/test_app/spec/javascripts/router_spec.js +121 -0
- data/test_app/spec/javascripts/support/jasmine.yml +20 -0
- data/{spec/test_app → test_app}/spec/spec_helper.rb +11 -5
- data/test_app/test/fixtures/.gitkeep +0 -0
- data/test_app/test/functional/.gitkeep +0 -0
- data/test_app/test/integration/.gitkeep +0 -0
- data/test_app/test/performance/browsing_test.rb +12 -0
- data/test_app/test/test_helper.rb +13 -0
- data/test_app/test/unit/.gitkeep +0 -0
- data/test_app/vendor/assets/javascripts/.gitkeep +0 -0
- data/test_app/vendor/assets/stylesheets/.gitkeep +0 -0
- data/test_app/vendor/plugins/.gitkeep +0 -0
- data/vendor/assets/javascripts/paloma/controller.js +10 -0
- data/vendor/assets/javascripts/paloma/controller_builder.js +55 -0
- data/vendor/assets/javascripts/paloma/engine.js +61 -0
- data/vendor/assets/javascripts/paloma/index.js +6 -0
- data/vendor/assets/javascripts/paloma/init.js +1 -0
- data/vendor/assets/javascripts/paloma/paloma.js +17 -0
- data/vendor/assets/javascripts/paloma/router.js +64 -0
- metadata +86 -94
- data/app/templates/_filters.js +0 -14
- data/app/templates/application/_locals.js +0 -13
- data/app/templates/controller/_locals.js +0 -19
- data/app/templates/controller/_manifest.js +0 -3
- data/app/templates/controller/action.js +0 -23
- data/app/templates/index.js +0 -3
- data/app/templates/namespace/_locals.js +0 -16
- data/app/templates/namespace/_manifest.js +0 -3
- data/app/views/paloma/_callback_hook.html.erb +0 -40
- data/lib/paloma/action_controller_filters.rb +0 -68
- data/lib/paloma/generators/add_generator.rb +0 -136
- data/lib/paloma/generators/setup_generator.rb +0 -38
- data/lib/paloma/rails/controller_generator.rb +0 -11
- data/spec/test_app/app/assets/javascripts/application.js +0 -16
- data/spec/test_app/app/assets/javascripts/paloma/_filters.js +0 -130
- data/spec/test_app/app/assets/javascripts/paloma/_locals.js +0 -17
- data/spec/test_app/app/assets/javascripts/paloma/bar/_filters.js +0 -96
- data/spec/test_app/app/assets/javascripts/paloma/bar/_locals.js +0 -1
- data/spec/test_app/app/assets/javascripts/paloma/bar/_manifest.js +0 -3
- data/spec/test_app/app/assets/javascripts/paloma/bar/another_basic_action.js +0 -5
- data/spec/test_app/app/assets/javascripts/paloma/bar/basic_action.js +0 -5
- data/spec/test_app/app/assets/javascripts/paloma/bar/different_params.js +0 -4
- data/spec/test_app/app/assets/javascripts/paloma/bar/yet_another_basic_action.js +0 -5
- data/spec/test_app/app/assets/javascripts/paloma/foo/_filters.js +0 -24
- data/spec/test_app/app/assets/javascripts/paloma/foo/_locals.js +0 -21
- data/spec/test_app/app/assets/javascripts/paloma/foo/_manifest.js +0 -3
- data/spec/test_app/app/assets/javascripts/paloma/foo/basic_action.js +0 -21
- data/spec/test_app/app/assets/javascripts/paloma/foo/skip_callback.js +0 -6
- data/spec/test_app/app/assets/javascripts/paloma/index.js +0 -6
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/_filters.js +0 -83
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/_manifest.js +0 -2
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/baz/_filters.js +0 -84
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/baz/_locals.js +0 -1
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/baz/_manifest.js +0 -2
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/baz/another_basic_action.js +0 -5
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/baz/basic_action.js +0 -5
- data/spec/test_app/app/assets/javascripts/paloma/sample_namespace/baz/yet_another_basic_action.js +0 -5
- data/spec/test_app/app/controllers/bar_controller.rb +0 -28
- data/spec/test_app/app/controllers/foo_controller.rb +0 -25
- data/spec/test_app/app/controllers/sample_namespace/baz_controller.rb +0 -23
- data/spec/test_app/app/views/layouts/application.html.erb +0 -12
- data/spec/test_app/config/routes.rb +0 -33
- data/spec/test_app/spec/generator_helper.rb +0 -87
- data/spec/test_app/spec/generators/paloma_generator_spec.rb +0 -199
- data/spec/test_app/spec/generators/rails_generator_spec.rb +0 -54
- data/spec/test_app/spec/javascripts/callback_spec.rb +0 -64
- data/spec/test_app/spec/javascripts/filters_spec.rb +0 -187
- data/spec/test_app/spec/javascripts/locals_spec.rb +0 -27
- data/spec/test_app/spec/javascripts/params_spec.rb +0 -116
- data/spec/test_app/spec/javascripts/skip_filters_spec.rb +0 -58
- data/spec/test_app/spec/javascripts/variable_container_spec.rb +0 -21
- data/vendor/assets/javascripts/paloma_core.js +0 -296
data/Changelog.md
CHANGED
@@ -1,76 +1,4 @@
|
|
1
|
-
Changelog
|
2
|
-
=
|
1
|
+
# Changelog
|
3
2
|
|
4
|
-
Version
|
5
|
-
|
6
|
-
* Pull #15: Handling flash on redirect.
|
7
|
-
|
8
|
-
Version 2.0.5
|
9
|
-
-
|
10
|
-
* Bug Fix: Stupid typo on `_callback_hook.html.erb`.
|
11
|
-
|
12
|
-
Version 2.0.4
|
13
|
-
-
|
14
|
-
* Bug Fix: Issue #4 - Passing json string on params.
|
15
|
-
|
16
|
-
Version 2.0.3
|
17
|
-
-
|
18
|
-
* Bug Fix: prevent javascript error when callback doesn't exist.
|
19
|
-
|
20
|
-
Version 2.0.2
|
21
|
-
-
|
22
|
-
* 'js(false)' will prevent Paloma from appending _callback_hook on the response.
|
23
|
-
|
24
|
-
Version 2.0.1
|
25
|
-
-
|
26
|
-
* Bug Fix: `params` is not passed when calling filters.
|
27
|
-
|
28
|
-
Version 2.0.0
|
29
|
-
-
|
30
|
-
* Change `js_callback` to `js` only.
|
31
|
-
* Request and Callback details are automatically included on `params` (example: controller, action, namespace, controller_path, etc...)
|
32
|
-
* Hooks for Rails controller and scaffold generators.
|
33
|
-
* Change `Paloma.callbacks['namespace/controller/action']` to `Paloma.callbacks['namespace/controller']['action']`.
|
34
|
-
* `paloma.js` file removed.
|
35
|
-
* `_callbacks.js` renamed to `_manifest.js`.
|
36
|
-
* `_local.js` renamed to `_locals.js`.
|
37
|
-
* Filters are now available (before_filter, after_filter, and around_filter).
|
38
|
-
* Skip filters are now available (skip_before_filter, skip_after_filter, skip_around_filter).
|
39
|
-
* Locals can now be easily accessible using the `_l` object.
|
40
|
-
* Issue javascript warning instead of javascript error when Paloma is not included on `application.js`.
|
41
|
-
* Codes are now inside closures, to prevent variable clashes.
|
42
|
-
|
43
|
-
Version 1.2.0
|
44
|
-
-
|
45
|
-
* AddGenerator with multiple actions: `rails g paloma:add namespace/controller action1 action2 action3`
|
46
|
-
|
47
|
-
Version 1.1.0
|
48
|
-
-
|
49
|
-
* Handle namespaced controllers: `rails g paloma:add namespace/controller action`
|
50
|
-
|
51
|
-
Version 1.0.0
|
52
|
-
-
|
53
|
-
* `AddGenerator` changed from `rails g paloma:add controller/action` to `rails g paloma:add controller action`
|
54
|
-
|
55
|
-
Version 0.0.8
|
56
|
-
-
|
57
|
-
* Bug Fix: _callback.js cannot find _local.js
|
58
|
-
|
59
|
-
Version 0.0.7
|
60
|
-
-
|
61
|
-
* Fix A Major Bug that copies the _local.js template instead of _callbacks.js
|
62
|
-
|
63
|
-
Version 0.0.6
|
64
|
-
-
|
65
|
-
* `SetupGenerator` moved from `paloma_generator.rb` to `paloma/generators/setup_generator.rb`
|
66
|
-
* `AddGenerator` moved from `paloma_generator` to `paloma/generators/add_generator.rb`
|
67
|
-
* Refactored both generators
|
68
|
-
* Test for both generators
|
69
|
-
|
70
|
-
Version 0.0.5
|
71
|
-
-
|
72
|
-
* Callbacks are executed after the DOM is ready
|
73
|
-
|
74
|
-
Version 0.0.4
|
75
|
-
-
|
76
|
-
* First release
|
3
|
+
## Version 3.0.0
|
4
|
+
* Rewrite Initial Release
|
data/README.md
CHANGED
@@ -1,25 +1,30 @@
|
|
1
|
-
Paloma
|
2
|
-
======
|
3
|
-
Paloma provides a sexy and logical way of organizing Rails javascript files.
|
4
|
-
Its core feature is a powerful yet simple way to execute page-specific javascript code.
|
1
|
+
# Paloma
|
5
2
|
|
6
|
-
But there are more sexy features Paloma has to offer!
|
7
3
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
## What's New?
|
5
|
+
Paloma (version 3) is almost a complete rewrite of the old version.
|
6
|
+
|
7
|
+
It is now simpler and it also gives more flexibility to the developers. Simplicity and flexibility are achieved by replacing the old callback thingy paradigm by a combination of `Router` and `Controller` components.
|
8
|
+
|
9
|
+
All the generator shits are also gone. So developers need not to follow specific folder structure or file name. And since there's no generated files or whatsoever, you can now code in vanilla javascript or **coffescript**! Yay!
|
10
|
+
|
11
|
+
**Basically, Paloma now provides a Controller for your javascript!**
|
12
|
+
|
13
|
+
## Advantages
|
14
|
+
* Choose what specific javascript code to run per page.
|
13
15
|
* Easily make ruby variables available on your javascript files.
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
|
18
|
+
## Quick Example
|
19
|
+
|
20
|
+
Paloma controller.
|
18
21
|
|
19
22
|
```javascript
|
20
|
-
Paloma.
|
21
|
-
|
22
|
-
|
23
|
+
var UsersController = Paloma.controller('Users');
|
24
|
+
|
25
|
+
// Executes when Rails User#new is rendered.
|
26
|
+
UsersController.prototype.new = function(){
|
27
|
+
alert('Hello Sexy User!' );
|
23
28
|
};
|
24
29
|
```
|
25
30
|
|
@@ -28,9 +33,7 @@ The Rails controller `app/controllers/users_controller.rb`:
|
|
28
33
|
```ruby
|
29
34
|
def UsersController < ApplicationController
|
30
35
|
def new
|
31
|
-
|
32
|
-
# No special function to call, the javascript callback will be executed automatically
|
33
|
-
# just for this specific action.
|
36
|
+
@user = User.new
|
34
37
|
end
|
35
38
|
end
|
36
39
|
```
|
@@ -43,8 +46,8 @@ Minimum Requirements
|
|
43
46
|
* Rails 3.1 or higher
|
44
47
|
|
45
48
|
|
46
|
-
Install
|
47
|
-
|
49
|
+
## Install
|
50
|
+
|
48
51
|
Without bundler:
|
49
52
|
```
|
50
53
|
sudo gem install paloma
|
@@ -55,169 +58,77 @@ With bundler, add this to your Gemfile:
|
|
55
58
|
gem 'paloma'
|
56
59
|
```
|
57
60
|
|
58
|
-
Setup
|
59
|
-
-
|
60
|
-
On setup, the `paloma` folder will be generated in `app/assets/javascripts/` containing its required files. Run:
|
61
|
-
```
|
62
|
-
rails g paloma:setup
|
63
|
-
```
|
64
61
|
|
65
62
|
Require `paloma` in your `application.js`:
|
66
63
|
```
|
67
64
|
//= require paloma
|
68
65
|
```
|
69
66
|
|
70
|
-
Basic Directory Structure
|
71
|
-
-
|
72
|
-
`paloma` folder contains the javascript callbacks.
|
73
|
-
|
74
|
-
* paloma
|
75
|
-
* [controller]
|
76
|
-
* [action].js
|
77
|
-
* [other_action].js
|
78
|
-
* [other_controller]
|
79
|
-
* [action].js
|
80
|
-
* [other_action].js
|
81
|
-
* [more_action].js
|
82
|
-
* [namespace]
|
83
|
-
* [controller]
|
84
|
-
* [action].js
|
85
|
-
|
86
|
-
Generators
|
87
|
-
-
|
88
67
|
|
89
|
-
|
90
|
-
```
|
91
|
-
rails g paloma:add [controller]
|
92
|
-
```
|
93
|
-
**Example:**
|
94
|
-
```
|
95
|
-
rails g paloma:add users
|
96
|
-
```
|
68
|
+
## Router
|
97
69
|
|
98
|
-
|
99
|
-
* /paloma
|
100
|
-
* /users
|
70
|
+
Router is responsible for mapping Rails controller/action to its equivalent Paloma controller/action.
|
101
71
|
|
72
|
+
By default all Rails controller/action will be mapped with a Paloma controller/action with the same resource name (controller name without the `Controller` suffix).
|
102
73
|
|
103
|
-
|
104
|
-
|
105
|
-
rails g paloma:add [controller] [action]
|
106
|
-
```
|
107
|
-
**Example:**
|
108
|
-
```
|
109
|
-
rails g paloma:add users new
|
110
|
-
```
|
74
|
+
Example:
|
75
|
+
* Response from `UsersController#new` will be mapped to `Users` Paloma controller and execute its `new` method.
|
111
76
|
|
112
|
-
|
113
|
-
* /paloma
|
114
|
-
* /users
|
115
|
-
* new.js
|
77
|
+
### Changing Controller
|
116
78
|
|
79
|
+
If you want to use a different Paloma Controller for a specific Rails controller, you can do the following:
|
117
80
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
**Example:**
|
123
|
-
```
|
124
|
-
rails g paloma:add users new create edit update
|
125
|
-
```
|
126
|
-
|
127
|
-
**Generates:**
|
128
|
-
* /paloma
|
129
|
-
* /users
|
130
|
-
* new.js
|
131
|
-
* create.js
|
132
|
-
* edit.js
|
133
|
-
* update.js
|
134
|
-
|
135
|
-
4. Generate namespaced controller and callbacks:
|
136
|
-
```
|
137
|
-
rails g paloma:add [namespace]/[controller] [action_1] [action_2] ... [action_n]
|
81
|
+
```javascript
|
82
|
+
// Instead of mapping Rails UsersController to Paloma Users
|
83
|
+
// it will be mapped to AdminUsers.
|
84
|
+
Paloma.router.resource('Users', {controller: 'AdminUsers'});
|
138
85
|
```
|
139
86
|
|
140
|
-
|
141
|
-
```
|
142
|
-
rails g paloma:add admin/users new
|
143
|
-
```
|
87
|
+
### Redirecting
|
144
88
|
|
145
|
-
|
146
|
-
* /paloma
|
147
|
-
* /admin
|
148
|
-
* /users
|
149
|
-
* new.js
|
150
|
-
|
151
|
-
**Notes:**
|
89
|
+
You can also redirect an action if you want it to be handled by a different method.
|
152
90
|
|
153
|
-
|
154
|
-
|
91
|
+
```javascript
|
92
|
+
// Instead of executing Paloma's `Users#new` it will execute
|
93
|
+
// `Registrations#signUp`.
|
94
|
+
Paloma.router.redirect('Users#new', {to: 'Registrations#signUp');
|
95
|
+
```
|
155
96
|
|
156
|
-
|
97
|
+
## Controller
|
157
98
|
|
158
|
-
|
159
|
-
-
|
160
|
-
By default Paloma will execute the callback that matches the current controller and action if it finds one.
|
161
|
-
For instance, if the current response is from the `new` action of the `Users` controller, then Paloma will try to execute `callbacks['users']['new']` if it exists.
|
99
|
+
Controller handles responses from Rails. A new controller instance is created by Paloma for every Rails controller/action that is executed.
|
162
100
|
|
163
|
-
|
101
|
+
### Creating Controller
|
164
102
|
|
165
|
-
|
103
|
+
A Controller is created or accessed (if already existing) using:
|
166
104
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
user.destroy
|
171
|
-
|
172
|
-
js false
|
173
|
-
end
|
174
|
-
```
|
175
|
-
|
176
|
-
`callbacks["controller"]["destroy"]` will not be executed.
|
177
|
-
|
178
|
-
2. Using other action's callback from the same controller.
|
105
|
+
```javascript
|
106
|
+
Paloma.controller('ControllerName');
|
107
|
+
```
|
179
108
|
|
180
|
-
|
181
|
-
def edit
|
182
|
-
@user = User.find params[:id]
|
183
|
-
js :new
|
184
|
-
end
|
185
|
-
```
|
109
|
+
It returns the constructor of your controller. It is just a normal constructor so you can do your OOP stuff.
|
186
110
|
|
187
|
-
This will execute `callback["controllers"]["new"]` instead of `callback["controllers"]["edit"]`.
|
188
|
-
|
189
|
-
3. Using other action's callback from other controller.
|
190
111
|
|
191
|
-
|
192
|
-
def index
|
193
|
-
@users = User.all
|
194
|
-
js :controller => 'clients', :action => 'index'
|
195
|
-
end
|
196
|
-
```
|
112
|
+
### Creating actions
|
197
113
|
|
198
|
-
|
114
|
+
Add instance methods to you Controller constructor to handle actions.
|
199
115
|
|
116
|
+
```javascript
|
117
|
+
var ArticlesController = Paloma.controller('ArticlesController');
|
200
118
|
|
201
|
-
|
119
|
+
ArticlesController.prototype.new = function(){
|
120
|
+
// Handle new articles
|
121
|
+
};
|
202
122
|
|
203
|
-
```ruby
|
204
|
-
class UsersController < ApplicationController
|
205
|
-
def destroy
|
206
|
-
@user = User.find params[:id]
|
207
|
-
@user.destroy
|
208
|
-
|
209
|
-
js :controller => 'admin/users', :action => :destroy
|
210
|
-
end
|
211
|
-
end
|
212
|
-
```
|
213
123
|
|
214
|
-
|
124
|
+
ArticlesController.prototype.edit = function(){
|
125
|
+
// Handle edit articles
|
126
|
+
};
|
127
|
+
```
|
215
128
|
|
129
|
+
## Passing Parameters
|
216
130
|
|
217
|
-
|
218
|
-
-
|
219
|
-
You can also pass parameters to the callback by passing a `:params` key to `js`. The passed parameters
|
220
|
-
will be available on the callback by the `params` object.
|
131
|
+
You can also pass parameters to Paloma by calling `js` before render in your Rails controller. You can access the parameters on your Paloma controller using `this.params` object.
|
221
132
|
|
222
133
|
**Example:**
|
223
134
|
|
@@ -227,235 +138,35 @@ def destroy
|
|
227
138
|
user = User.find params[:id]
|
228
139
|
user.destroy
|
229
140
|
|
230
|
-
js :
|
141
|
+
js :id => user.id
|
231
142
|
end
|
232
143
|
```
|
233
144
|
|
234
|
-
|
235
|
-
```javascript
|
236
|
-
Paloma.callbacks['users']['destroy'] = function(params){
|
237
|
-
var id = params['user_id'];
|
238
|
-
alert('User ' + id + ' deleted.');
|
239
|
-
};
|
240
|
-
```
|
241
|
-
|
242
|
-
|
243
|
-
Default Parameters
|
244
|
-
-
|
245
|
-
`params['controller']` - controller name without its namespace.
|
246
|
-
|
247
|
-
`params['namespace']` - controller's namespace name.
|
248
|
-
|
249
|
-
`params['action']` - controller's action that triggers the filter or callback.
|
250
|
-
|
251
|
-
`params['controller_path']` - controller name with its namespace.
|
252
|
-
|
253
|
-
`params['callback_controller']` - callback's controller name without its namespace.
|
254
|
-
|
255
|
-
`params['callback_namespace']` - callback's namespace name.
|
256
|
-
|
257
|
-
`params['callback_action']` - callback's action name.
|
258
|
-
|
259
|
-
`params['callback_controller_path']` - callback's controller with its namespace.
|
260
|
-
|
261
|
-
## Filters
|
262
|
-
|
263
|
-
This is almost similar to Rails controller filters. These are functions executed either `before`, `after`,
|
264
|
-
or even `around` (before and after) a Paloma callback is executed.
|
265
|
-
|
266
|
-
Filters are defined on `_filters.js` files in Paloma's root folder, namespace folder, or controller folder.
|
267
|
-
|
268
|
-
### Syntax
|
145
|
+
Paloma controller.
|
269
146
|
|
270
147
|
```javascript
|
271
|
-
filter.as('filter name').before_all().perform(function(params){
|
272
|
-
alert("I'm a before filter!");
|
273
|
-
});
|
274
|
-
|
275
|
-
filter.as('another filter').after_all().perform(function(params){
|
276
|
-
alert("I'm an after filter");
|
277
|
-
});
|
278
|
-
```
|
279
|
-
|
280
|
-
### Specify Actions To Filter
|
281
|
-
|
282
|
-
1. For all actions.
|
283
|
-
|
284
|
-
```javascript
|
285
|
-
filter.as('name').before_all()
|
286
|
-
filter.as('name').after_all()
|
287
|
-
filter.as('name').around_all()
|
288
|
-
```
|
289
|
-
|
290
|
-
2. Only for specific action/s.
|
291
|
-
|
292
|
-
```javascript
|
293
|
-
filter.as('name').before('new', 'edit', 'update')
|
294
|
-
filter.as('name').after('destroy')
|
295
|
-
filter.as('name').around('show')
|
296
|
-
```
|
297
|
-
|
298
|
-
3. Except for specific action/s.
|
299
|
-
|
300
|
-
```javascript
|
301
|
-
filter.as('name').except_before('new')
|
302
|
-
filter.as('name').except_after('destroy', 'edit')
|
303
|
-
filter.as('name').except_around('show')
|
304
|
-
```
|
305
|
-
|
306
|
-
### Filter Inheritance
|
307
|
-
|
308
|
-
All `_filters.js` inherit filters defined on the global `_filters.js` file.
|
309
|
-
Controller's `_filters.js` will also inherit filters defined on its namespace `_filters.js` if it exists.
|
310
|
-
|
311
148
|
|
312
|
-
|
149
|
+
var UsersController = Paloma.controller('Users');
|
313
150
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
Before filters, as you've guessed, are executed before the callback is called.
|
318
|
-
The order of execution is based on the order of declaration.
|
319
|
-
After before filters, around filters are executed then the callback is finally executed.
|
320
|
-
After filters are called after the callback is executed, then it will execute the around filters again.
|
321
|
-
|
322
|
-
### Shared Variable Between Filter and Callback
|
323
|
-
|
324
|
-
Automatically, filters has an access to the `params` object passed via the `js` ruby method.
|
325
|
-
But you can also make a variable visible both on a filter and a callback using the `_x` object.
|
326
|
-
|
327
|
-
**Example:**
|
328
|
-
|
329
|
-
on `_filters.js`:
|
330
|
-
|
331
|
-
```javascript
|
332
|
-
filter.as('filter name').before('new').perform(function(params){
|
333
|
-
_x.sharedVariable = "Sexy Paloma";
|
334
|
-
});
|
335
|
-
```
|
336
|
-
|
337
|
-
on `new.js`:
|
338
|
-
|
339
|
-
```javascript
|
340
|
-
Paloma.callbacks['controller']['new'] = function(params){
|
341
|
-
alert(_x.sharedVariable); // outputs "Sexy Paloma";
|
342
|
-
});
|
343
|
-
```
|
344
|
-
|
345
|
-
### Skipping Filters
|
346
|
-
|
347
|
-
You can skip filters using the `skip_*_filter` or `skip_*_filters` command.
|
348
|
-
You can also specify which action or actions the filter skip is applicable using `only` or `except` command.
|
349
|
-
|
350
|
-
```javascript
|
351
|
-
filter.skip_before_filter('filter A'); // skip 'filter A' for all actions
|
352
|
-
filter.skip_after_filters('filter A', 'filter B').only('new', 'edit'); // skip 'filter A' and 'filter B' for 'new' and 'edit' actions.
|
353
|
-
filter.skip_around_filter('filter A').except('destroy'); // skip 'filter A' for all actions except for 'destroy'.
|
354
|
-
```
|
355
|
-
|
356
|
-
##Locals
|
357
|
-
|
358
|
-
Locals are variables or methods which can be made locally available within a controller or a namespace. Locals can also be made available throughout the whole Paloma files (globally).
|
359
|
-
|
360
|
-
The motivation of Locals is to organize helper methods and helper variables within a namespace or controller.
|
361
|
-
|
362
|
-
1. **Application-wide Locals**
|
363
|
-
|
364
|
-
Defined on `paloma/_locals.js`.
|
365
|
-
This contains methods and variables that are intended to be available globally.
|
366
|
-
|
367
|
-
|
368
|
-
2. **Namespace-wide Locals**
|
369
|
-
|
370
|
-
Defiend on `paloma/namespace/_locals.js`.
|
371
|
-
This contains methods and variables that are intended to be available on the specific namespace only.
|
372
|
-
|
373
|
-
|
374
|
-
3. **Controller-wide Locals**
|
375
|
-
|
376
|
-
Defined on `paloma/controller/_locals.js` or `paloma/namespace/controller/_locals.js`.
|
377
|
-
This contains methods and variables that are intended to be available on the specific controller only.
|
378
|
-
|
379
|
-
|
380
|
-
###Creating Locals
|
381
|
-
Locals can be created using the `locals` object inside `_filters.js` file.
|
382
|
-
|
383
|
-
**Example:**
|
384
|
-
|
385
|
-
```javascript
|
386
|
-
locals.helperMethod = function(){
|
387
|
-
return "Hello World";
|
388
|
-
};
|
389
|
-
|
390
|
-
locals.helperVariable = "WOW!";
|
391
|
-
```
|
392
|
-
|
393
|
-
###Accessing Locals
|
394
|
-
Locals can be accessed in your filter and callback files using the `_l` object.
|
395
|
-
|
396
|
-
**Example**
|
397
|
-
|
398
|
-
```javascript
|
399
|
-
Paloma.callbacks['users']['new'] = function(params){
|
400
|
-
alert("Hello Sexy User");
|
401
|
-
|
402
|
-
_l.helperMethod();
|
403
|
-
|
404
|
-
console.log(_l.helperVariable);
|
151
|
+
UsersController.prototype.destroy = function(){
|
152
|
+
alert('User ' + this.params['id'] + ' is deleted.');
|
405
153
|
};
|
406
154
|
```
|
407
155
|
|
408
|
-
|
409
|
-
Sometimes there is a need to use other's local methods and variables.
|
410
|
-
You can achieve this by using the `Paloma.locals` object or its alias `_L`.
|
411
|
-
|
412
|
-
**Example**
|
413
|
-
```javascript
|
414
|
-
Paloma.callbacks['users']['new'] = function(params){
|
415
|
-
_L.otherController.helperMethod(); // accessing local helperMethod() of the otherController
|
416
|
-
_L['otherController'].helperVariable;
|
417
|
-
}
|
418
|
-
```
|
419
|
-
|
420
|
-
|
421
|
-
###Locals Inheritance
|
422
|
-
`_locals.js` inherits locals from its parent `_locals.js`, either from namespace or application-wide.
|
423
|
-
You can also override locals inherited from parents.
|
424
|
-
|
425
|
-
**Example**
|
426
|
-
|
427
|
-
`paloma/_locals.js` contains:
|
428
|
-
```javascript
|
429
|
-
locals.globalMethod = function(){ console.log("I'm from Global"); }
|
430
|
-
```
|
156
|
+
## Preventing Paloma
|
431
157
|
|
432
|
-
`
|
433
|
-
```javascript
|
434
|
-
locals.namespaceMethod = function(){ console.log("I'm from Namespace"); }
|
435
|
-
locals.anotherNamespaceMethod = function(){ console.log("I'm also from Namespace"); }
|
436
|
-
```
|
158
|
+
If you want to prevent Paloma from executing in a certain Rails controller action you can do it by passing `false` to `js` command.
|
437
159
|
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
160
|
+
```ruby
|
161
|
+
def edit
|
162
|
+
@user = User.find params[:id]
|
163
|
+
js false
|
164
|
+
end
|
442
165
|
```
|
443
166
|
|
444
|
-
|
445
|
-
So you can do something like this inside the controller callback files (or filter files):
|
167
|
+
## Execution Chains
|
446
168
|
|
447
|
-
|
448
|
-
Paloma.callbacks['namespace/controller']['action'] = function(params){
|
449
|
-
_l.controllerMethod(); // outputs "I'm from Controller"
|
450
|
-
_l.namespacedMethod(); // outputs "I'm from Namespace"
|
451
|
-
_l.globalMethod(); // outputs "I'm from Global"
|
452
|
-
_l.anotherNamespacedMethod(); // outputs "Override!"
|
453
|
-
};
|
454
|
-
```
|
455
|
-
|
456
|
-
Callback Chains
|
457
|
-
-
|
458
|
-
Callback chains are created after a redirect action. The chain will continue to increase its length until a render action is detected.
|
169
|
+
Chains are created after a redirect action. The chain will continue to increase its length until a render action is detected.
|
459
170
|
|
460
171
|
**Example:**
|
461
172
|
|
@@ -473,17 +184,19 @@ def third_action
|
|
473
184
|
end
|
474
185
|
```
|
475
186
|
|
476
|
-
A request for `first_action` will lead to 2 redirects until it reaches the `third_action` and renders a result on the browser. When the `third_action` renders its response, Paloma will
|
187
|
+
A request for `first_action` will lead to 2 redirects until it reaches the `third_action` and renders a result on the browser. When the `third_action` renders its response, Paloma will process all the request starting from `first_action` up to `third_action`.
|
477
188
|
|
478
|
-
The order of execution will be `[controllers]/first_action` first, then `[controllers]/second_action`, and finally `[controllers]/third_action`.
|
479
189
|
|
190
|
+
## Gotchas
|
480
191
|
|
481
|
-
|
482
|
-
-
|
483
|
-
* Callbacks will not be executed if the response is `js`, `json`, `xml` or any other format except `html`.
|
484
|
-
This will not work: `render "something.js.erb"`
|
192
|
+
* Paloma will not execute if the response is `js`, `json`, `xml` or any other format except `html`.
|
485
193
|
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
194
|
+
For example: `render "something.js.erb"`
|
195
|
+
|
196
|
+
|
197
|
+
## Where to put code?
|
198
|
+
|
199
|
+
Again, Paloma is now flexible and doesn't force developers to follow specific folder structure.
|
200
|
+
You have the freedom to create controllers and routes anywhere in your javascript code.
|
201
|
+
|
202
|
+
Personally, I prefer having a `routes.js` file to contain all the route declaration, and a javascript file for each controller.
|