poke_js 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +108 -7
- data/app/assets/javascripts/poke_js.js +23 -8
- data/app/views/layouts/application.js.erb +3 -5
- data/lib/poke_js/version.rb +1 -1
- data/lib/poke_js/view_helpers.rb +14 -5
- data/poke_js.gemspec +2 -2
- metadata +6 -5
data/README.md
CHANGED
@@ -1,10 +1,42 @@
|
|
1
1
|
# PokeJs
|
2
|
+
Auto-magical scaffolding for the [Garber-Irish Implementation](http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution) way of organizing your javascript.
|
2
3
|
|
3
|
-
|
4
|
+
## Purpose
|
5
|
+
Javascript is hard to organize and debugging ajax is a mess. This is one method to organizing your javascript neatly by mirroring the controllers and having all the JS outside of your HTML views.
|
4
6
|
|
5
|
-
##
|
7
|
+
## How it works
|
8
|
+
### Setup your namespace
|
9
|
+
```javascript
|
10
|
+
APP = {
|
11
|
+
all: {
|
12
|
+
html: {
|
13
|
+
init: function() {
|
14
|
+
}
|
15
|
+
}
|
16
|
+
},
|
17
|
+
demos: {
|
18
|
+
html: {
|
19
|
+
init: function() {
|
20
|
+
},
|
21
|
+
demo_action: function() {
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
```
|
27
|
+
### What happens
|
28
|
+
After, requests to `demos#demo_action` with format `html` will call the following functions (if they exist):
|
29
|
+
* `APP.all.html.init`
|
30
|
+
* `APP.demos.html.init`
|
31
|
+
* `APP.demos.html.demo_action` (with parameters if given)
|
32
|
+
|
33
|
+
`js` format is also supported, i.e.:
|
34
|
+
* `APP.all.js.init`
|
35
|
+
* `APP.demos.js.init`
|
36
|
+
* `APP.demos.js.demo_action` (with parameters if given)
|
6
37
|
|
7
|
-
|
38
|
+
## Installation
|
39
|
+
Add this line to your application's `Gemfile`:
|
8
40
|
|
9
41
|
gem 'poke_js'
|
10
42
|
|
@@ -12,10 +44,79 @@ And then execute:
|
|
12
44
|
|
13
45
|
$ bundle
|
14
46
|
|
15
|
-
|
47
|
+
Add this to your `app/assets/javascripts/application.js`
|
48
|
+
|
49
|
+
//= require poke_js
|
50
|
+
|
51
|
+
Make sure your `app/views/layouts/application.html.erb` (and all your other layouts) looks like this:
|
52
|
+
```erb
|
53
|
+
<html>
|
54
|
+
<head>… <%= poke %> …</head>
|
55
|
+
<body data-controller="<%= poke_js_template.first %>" data-action="<%= poke_js_template.last %>">
|
56
|
+
…
|
57
|
+
</body>
|
58
|
+
</html>
|
59
|
+
```
|
60
|
+
|
61
|
+
## Basic Use
|
62
|
+
I like to have a JS file for every controller in `app/assets/javascripts/controllers`. Like so:
|
63
|
+
|
64
|
+
`app/assets/javascripts/controllers/demos.js`:
|
65
|
+
```javascript
|
66
|
+
(function() {
|
67
|
+
var demos = APP.define('demos', {
|
68
|
+
html: {
|
69
|
+
edit: function(params) {
|
70
|
+
alert(params.alert_message);
|
71
|
+
}
|
72
|
+
},
|
73
|
+
|
74
|
+
js: {
|
75
|
+
new: function(params) {
|
76
|
+
console.log(params.log_message);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
});
|
80
|
+
})();
|
81
|
+
```
|
82
|
+
|
83
|
+
`APP.define()` extends or creates the namespace `APP.demos`
|
84
|
+
and returns it. This allows me to access `APP.demos` through
|
85
|
+
the `demos` variable.
|
86
|
+
|
87
|
+
You can also use the traditional hash
|
88
|
+
namespacing shown in the [Setup your namespace](https://github.com/s12chung/poke_js#setup-your-namespace) section.
|
89
|
+
|
90
|
+
### HTML
|
91
|
+
So if a `html` request is sent to `demos#edit`, `APP.demos.html.edit` is called with the HTML view rendering.
|
92
|
+
|
93
|
+
### Javascript
|
94
|
+
For a `js` request sent to `demos#new`, `APP.demos.js.new` is called and nothing else happens.
|
95
|
+
|
96
|
+
### Passing parameters
|
97
|
+
__Optional__ Parameters are passed from a JSON DSL (such as [jbuilder](https://github.com/rails/jbuilder/)) and is passed as the `params` object to the function. You can pass any JSON object.
|
16
98
|
|
17
|
-
|
99
|
+
#### HTML
|
100
|
+
`app/views/demos/edit_params.js.jbuilder`:
|
101
|
+
```ruby
|
102
|
+
json.alert_message "ploop"
|
103
|
+
```
|
104
|
+
so
|
105
|
+
```javascript
|
106
|
+
APP.demos.html.edit({ alert_message: "ploop" });
|
107
|
+
```
|
108
|
+
is called automatically.
|
18
109
|
|
19
|
-
|
110
|
+
#### Javascript
|
111
|
+
`app/views/demos/new.js.jbuilder`:
|
112
|
+
```ruby
|
113
|
+
json.log_message "loggggggggggggg"
|
114
|
+
```
|
115
|
+
so
|
116
|
+
```javascript
|
117
|
+
APP.demos.js.new({ log_message: "loggggggggggggg" });
|
118
|
+
```
|
119
|
+
is called automatically.
|
20
120
|
|
21
|
-
|
121
|
+
## Advanced Use
|
122
|
+
To be written...
|
@@ -18,6 +18,20 @@ POKE = {
|
|
18
18
|
return $.extend(found_namespace, definition);
|
19
19
|
},
|
20
20
|
|
21
|
+
get_action_namespace: function(controller, format) {
|
22
|
+
var levels = controller.split('/');
|
23
|
+
levels.push(format);
|
24
|
+
|
25
|
+
var current_namespace = APP;
|
26
|
+
|
27
|
+
for (var i=0;i<levels.length;i++) {
|
28
|
+
var level = levels[i];
|
29
|
+
if (POKE.blank(current_namespace[level]))
|
30
|
+
return false;
|
31
|
+
current_namespace = current_namespace[level];
|
32
|
+
}
|
33
|
+
return current_namespace;
|
34
|
+
},
|
21
35
|
|
22
36
|
exec_all: function(controller, format, action) {
|
23
37
|
POKE.exec("all", format);
|
@@ -25,13 +39,12 @@ POKE = {
|
|
25
39
|
POKE.exec(controller, format, action);
|
26
40
|
},
|
27
41
|
exec: function(controller, format, action) {
|
28
|
-
var
|
29
|
-
action = (action === undefined) ? "init" : action;
|
42
|
+
var action_namespace = POKE.get_action_namespace(controller, format)
|
30
43
|
|
31
|
-
if (
|
32
|
-
|
33
|
-
var funct =
|
34
|
-
params =
|
44
|
+
if ($.type(action_namespace) === "object") {
|
45
|
+
action = (action === undefined) ? "init" : action;
|
46
|
+
var funct = action_namespace[action],
|
47
|
+
params = action_namespace[action + "_params"];
|
35
48
|
|
36
49
|
if ($.isFunction(funct))
|
37
50
|
funct(params);
|
@@ -43,7 +56,7 @@ POKE = {
|
|
43
56
|
}
|
44
57
|
};
|
45
58
|
|
46
|
-
if (POKE.blank(window["APP"]))
|
59
|
+
if (POKE.blank(window["APP"])) {
|
47
60
|
APP = {
|
48
61
|
namespace_string: function(namespace_string) {
|
49
62
|
return "APP." + namespace_string;
|
@@ -55,4 +68,6 @@ if (POKE.blank(window["APP"]))
|
|
55
68
|
return POKE.define(APP.namespace_string(namespace_string), definition);
|
56
69
|
}
|
57
70
|
};
|
58
|
-
|
71
|
+
}
|
72
|
+
$(POKE.init);
|
73
|
+
$(document).on('page:change', POKE.init);
|
@@ -1,7 +1,5 @@
|
|
1
1
|
(function() {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
APP[controller][format][action + '_params'] = <%= yield %>;
|
6
|
-
POKE.exec_all(controller, format, action);
|
2
|
+
<% controller, action = poke_js_template %>
|
3
|
+
APP.<%= controller.gsub("/", ".") %>.js.<%= action %>_params = <%= yield %>;
|
4
|
+
POKE.exec_all('<%= poke_js_template[0] %>', 'js', '<%= poke_js_template[1] %>');
|
7
5
|
})();
|
data/lib/poke_js/version.rb
CHANGED
data/lib/poke_js/view_helpers.rb
CHANGED
@@ -28,15 +28,15 @@ module PokeJs
|
|
28
28
|
if lookup_context.template_exists? "#{controller}/#{action}_params"
|
29
29
|
javascript_tag do
|
30
30
|
raw %Q/
|
31
|
-
|
32
|
-
|
31
|
+
APP.#{controller.gsub("/", ".")}.html.#{action}_params = #{ render :template => "#{controller}/#{action}_params" };
|
32
|
+
/
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
36
36
|
elsif format == :js
|
37
37
|
content_for :head do
|
38
38
|
javascript_tag do
|
39
|
-
render :template => "#{controller}/#{action}", :formats => [:js], :layout => "layouts/application"
|
39
|
+
"$(function(){#{render :template => "#{controller}/#{action}", :formats => [:js], :layout => "layouts/application"}});".html_safe
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -51,9 +51,18 @@ module PokeJs
|
|
51
51
|
|
52
52
|
private
|
53
53
|
def extract_template(template)
|
54
|
-
extracted_template = [
|
54
|
+
extracted_template = [controller_path, action_name]
|
55
55
|
if template
|
56
|
-
array = template.class == Array
|
56
|
+
array = if template.class == Array
|
57
|
+
template
|
58
|
+
else
|
59
|
+
split = template.to_s.split('/')
|
60
|
+
template_controller = split[0..-2]
|
61
|
+
unless template_controller.empty?
|
62
|
+
template_controller = [template_controller.join('/')]
|
63
|
+
end
|
64
|
+
template_controller + [split.last]
|
65
|
+
end
|
57
66
|
if array.size == 1
|
58
67
|
extracted_template[1] = array.first
|
59
68
|
else
|
data/poke_js.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = PokeJs::VERSION
|
9
9
|
gem.authors = ["s12chung"]
|
10
10
|
gem.email = ["steve@placemarklist.com"]
|
11
|
-
gem.description = %q{
|
12
|
-
gem.summary = %q{
|
11
|
+
gem.description = %q{ Auto-magical scaffolding for the Garber-Irish Implementation way of organizing your javascript. }
|
12
|
+
gem.summary = %q{ Auto-magical scaffolding for the Garber-Irish Implementation way of organizing your javascript. }
|
13
13
|
gem.homepage = ""
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: poke_js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -43,8 +43,8 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
-
description: ! '
|
47
|
-
|
46
|
+
description: ! ' Auto-magical scaffolding for the Garber-Irish Implementation way
|
47
|
+
of organizing your javascript. '
|
48
48
|
email:
|
49
49
|
- steve@placemarklist.com
|
50
50
|
executables: []
|
@@ -85,6 +85,7 @@ rubyforge_project:
|
|
85
85
|
rubygems_version: 1.8.24
|
86
86
|
signing_key:
|
87
87
|
specification_version: 3
|
88
|
-
summary:
|
88
|
+
summary: Auto-magical scaffolding for the Garber-Irish Implementation way of organizing
|
89
|
+
your javascript.
|
89
90
|
test_files: []
|
90
91
|
has_rdoc:
|