js_hooks 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +10 -4
- data/lib/js_hooks/controller.rb +16 -3
- data/lib/js_hooks/version.rb +1 -1
- data/vendor/assets/javascripts/js_hooks.js +2 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adeb9388407f9a822354ef1354aea2f819eb8409
|
4
|
+
data.tar.gz: b5d0ad3fc47bcd2319929b5adeb3d4c0e12b3113
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ef9f8f095bbefcc463ba7448dcd447e8b0e15f0f959281c56f82c04dcc8003280788ff8fe56add281d8349e2e2fb5d69cd70e07b018df9086f536453f65dd1e
|
7
|
+
data.tar.gz: 5cce48c9949e703361325f5f199919e32865391077300b5959b632a3db7f2b6a566c9469a7862f68ab237765e1fedb826b231011631c5f2d80ad1d0bfe599113
|
data/README.md
CHANGED
@@ -54,17 +54,17 @@ This ``init`` function will be called automatically whenever the ``UsersControll
|
|
54
54
|
By default, the gem invokes the hooks related to the current controller, but hooks can be added if they are not named
|
55
55
|
after the current controller. For example, in your view, add:
|
56
56
|
|
57
|
-
|
57
|
+
<% add_js_hook( :users, :clients ) %>
|
58
58
|
--> Users.init(); Clients.init()
|
59
59
|
|
60
60
|
By default, the ``init`` function is called for each hook. However, a suffix can be added to the ``init`` method to refine
|
61
61
|
the initialization process:
|
62
62
|
|
63
|
-
|
63
|
+
<% add_js_hook( :users, :clients, users: 'action' ) %>
|
64
64
|
--> Users.init(); Clients.init(); Users.initAction();
|
65
65
|
|
66
|
-
|
67
|
-
--> Clients.init(); Users.init(); Users.
|
66
|
+
<% add_js_hook( :clients, users: [:default, 'show', 'edit'] ) %>
|
67
|
+
--> Clients.init(); Users.init(); Users.initShow(); Users.initEdit();
|
68
68
|
|
69
69
|
Three hooks are added by default: one with 'application' as name, one with the controller as name and one last
|
70
70
|
with the controller as name and template as suffix.
|
@@ -73,6 +73,12 @@ So, by default, the following methods will be called if they exist:
|
|
73
73
|
|
74
74
|
GET /users/1 --> Application.init(); Users.init(); Users.initShow()
|
75
75
|
|
76
|
+
A hook can be deleted by passing a false value as suffix
|
77
|
+
|
78
|
+
GET /users/1
|
79
|
+
<% add_js_hook( :clients, users: false) %>
|
80
|
+
--> Application.init(); Clients.init();
|
81
|
+
|
76
82
|
## Contributing
|
77
83
|
|
78
84
|
1. Fork it
|
data/lib/js_hooks/controller.rb
CHANGED
@@ -2,6 +2,7 @@ require 'abstract_controller/helpers'
|
|
2
2
|
|
3
3
|
module JsHooks
|
4
4
|
module Controller
|
5
|
+
PREFIX = 'init'
|
5
6
|
extend ActiveSupport::Concern
|
6
7
|
|
7
8
|
module ClassMethods
|
@@ -30,7 +31,7 @@ module JsHooks
|
|
30
31
|
# add_js_hook( :users, :clients, users: 'action' )
|
31
32
|
# --> Users.init(); Clients.init(); Users.initAction();
|
32
33
|
#
|
33
|
-
# add_js_hook( :clients, users: [
|
34
|
+
# add_js_hook( :clients, users: [:default, 'action1', 'action2'] )
|
34
35
|
# --> Clients.init(); Users.init(); Users.initAction1(); Users.initAction2();
|
35
36
|
#
|
36
37
|
# Three hooks are added by default: one with 'application' as name, one with the controller as name and one last
|
@@ -38,6 +39,11 @@ module JsHooks
|
|
38
39
|
# So, by default, the following methods will be called if they exist:
|
39
40
|
#
|
40
41
|
# GET /users/1 --> Application.init(); Users.init(); Users.initShow()
|
42
|
+
#
|
43
|
+
# A hook can be deleted by passing a false value as suffix
|
44
|
+
# GET /users/1
|
45
|
+
# add_js_hook( :clients, users: false)
|
46
|
+
# --> Application.init(); Clients.init();
|
41
47
|
def add_js_hook(*args)
|
42
48
|
opts = args.extract_options!
|
43
49
|
|
@@ -45,15 +51,22 @@ module JsHooks
|
|
45
51
|
args.each do |hook|
|
46
52
|
hook = hook.to_s.camelize
|
47
53
|
js_hooks[hook] ||= []
|
48
|
-
js_hooks[hook] = (js_hooks[hook] + [
|
54
|
+
js_hooks[hook] = (js_hooks[hook] + [PREFIX]).uniq
|
49
55
|
end
|
50
56
|
|
51
57
|
# Suffix init method
|
52
58
|
opts.each_pair do |hook, methods|
|
53
59
|
hook = hook.to_s.camelize
|
54
60
|
js_hooks[hook] ||= []
|
61
|
+
remove_default = false
|
55
62
|
if methods
|
56
|
-
|
63
|
+
methods = [methods].flatten.map! do |m|
|
64
|
+
remove_default ||= !m
|
65
|
+
m = '' if m == :default
|
66
|
+
"#{PREFIX}#{m.to_s.camelize}"
|
67
|
+
end
|
68
|
+
js_hooks[hook] = (js_hooks[hook] + methods).uniq
|
69
|
+
js_hooks[hook].delete(PREFIX) if remove_default
|
57
70
|
else
|
58
71
|
js_hooks.delete(hook)
|
59
72
|
end
|
data/lib/js_hooks/version.rb
CHANGED
@@ -26,17 +26,14 @@
|
|
26
26
|
|
27
27
|
var methods = hooks[hookClass];
|
28
28
|
if (!$.isArray(methods)) {
|
29
|
-
methods = [
|
29
|
+
methods = [methods];
|
30
30
|
}
|
31
31
|
|
32
32
|
for (var i = 0; i < methods.length; i++) {
|
33
33
|
var method = methods[i];
|
34
|
-
if (method ===
|
35
|
-
method = '';
|
36
|
-
} else if (method === false) {
|
34
|
+
if (method === false) {
|
37
35
|
continue;
|
38
36
|
}
|
39
|
-
method = 'init' + method;
|
40
37
|
|
41
38
|
if ($.isFunction(hook[method])) {
|
42
39
|
// if(console && console.debug) console.debug('Calling ' + hookClass + '.' + method + '();');
|