js-namespace-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +51 -0
- data/app/assets/javascripts/js-namespace-rails.js +72 -0
- data/lib/js-namespace-rails.rb +1 -0
- data/lib/js_namespace_rails.rb +7 -0
- data/lib/js_namespace_rails/action_view/helpers/asset_tag_helper.rb +22 -0
- data/lib/js_namespace_rails/engine.rb +9 -0
- data/lib/js_namespace_rails/version.rb +3 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6549d1e9a3e5654e0478a1d1b653dcb873f53961
|
4
|
+
data.tar.gz: 28d01f3641b7693489b06a9afdf8f651c88c76e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 945957b13386916a493cfd9c6805ec1e66b83622b6396fae2d90a4d3b240775dac471005a6cbac15ee4fac65771f5e09e4ea279ad785457199736c544c07342a
|
7
|
+
data.tar.gz: 2adea15411985e2282a441296179d6c7c5b765d60805a120cfb5924a9b115714cedce9c789360b3c07b4662925d59325d64545a47daf056cbdd8652ed2d77895
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# JsNamespaceRails
|
2
|
+
|
3
|
+
|
4
|
+
Rails's asset pipeline compiles all of js file into a single file which is executed on all pages.
|
5
|
+
There has a problem, some time we want to execute selective code on specific page, but asset pipeline doesn't support.
|
6
|
+
js-namespace-rails can handle this problem by using it's method to namespace and selectively execute certain javascript depending on which Rails controller action is active.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'js-namespace-rails', github: 'falm/js-namepsace-rails'
|
14
|
+
```
|
15
|
+
|
16
|
+
## Setup
|
17
|
+
|
18
|
+
Require js-namespace-rails file in application.js or other bundle,notice js-namespace-rails depends on jquery so you need require 'jquery' before
|
19
|
+
|
20
|
+
``` js
|
21
|
+
//= require jquery
|
22
|
+
//= require js-namespace-rails
|
23
|
+
```
|
24
|
+
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
Assume your project have articles_controller
|
28
|
+
``` ruby
|
29
|
+
class ArticlesController < ApplicationController
|
30
|
+
def index
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
And its corresponding js file app/assets/javascripts/articles.js.erb
|
35
|
+
|
36
|
+
then you just need to write blew into the js file
|
37
|
+
``` js
|
38
|
+
// app/assets/javascripts/articles.js.erb
|
39
|
+
JsSpace.on('articles', {
|
40
|
+
init: function(){
|
41
|
+
console.log('common logic of article in here');
|
42
|
+
},
|
43
|
+
index: function(){
|
44
|
+
console.log('logic of index action in here');
|
45
|
+
}
|
46
|
+
});
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
|
2
|
+
(function($, win){
|
3
|
+
|
4
|
+
'use strict';
|
5
|
+
|
6
|
+
function JsNamespace(){
|
7
|
+
|
8
|
+
var self = this;
|
9
|
+
|
10
|
+
self.INIT = 'init';
|
11
|
+
|
12
|
+
self.PARAMS_CONTAINER = '.executive-isolation-params';
|
13
|
+
|
14
|
+
self.contrllerList = {};
|
15
|
+
|
16
|
+
self.on = function(controllerName, obj){
|
17
|
+
self.contrllerList[controllerName] = obj;
|
18
|
+
};
|
19
|
+
|
20
|
+
self.fetchParams = function(){
|
21
|
+
var out_options = {};
|
22
|
+
$(self.PARAMS_CONTAINER).each(function(i, ele){
|
23
|
+
var options = $(ele).data('options');
|
24
|
+
if(options){
|
25
|
+
out_options = $.extend(out_options, options);
|
26
|
+
}
|
27
|
+
});
|
28
|
+
return out_options;
|
29
|
+
};
|
30
|
+
|
31
|
+
self.setInit = function(controllerName, actionName){
|
32
|
+
var $body = $('body');
|
33
|
+
$body.attr('data-controller', controllerName);
|
34
|
+
$body.attr('data-action', actionName);
|
35
|
+
};
|
36
|
+
|
37
|
+
self.init = function(controllerName, actionName){
|
38
|
+
|
39
|
+
var params = [self.fetchParams()];
|
40
|
+
|
41
|
+
var activeController = self.contrllerList[controllerName];
|
42
|
+
|
43
|
+
if( activeController !== undefined && typeof activeController === 'object') {
|
44
|
+
|
45
|
+
if(activeController[self.INIT] !== undefined && typeof activeController[self.INIT] === 'function'){
|
46
|
+
activeController.init(params);
|
47
|
+
}
|
48
|
+
|
49
|
+
if(activeController[actionName] !== undefined && typeof activeController[actionName] === 'function'){
|
50
|
+
activeController[actionName].apply(null, params);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
var jsNamespace = new JsNamespace();
|
57
|
+
|
58
|
+
$(document).ready(function(){
|
59
|
+
|
60
|
+
var $body = $('body');
|
61
|
+
|
62
|
+
var controllerName = $body.data('controller');
|
63
|
+
|
64
|
+
var actionName = $body.data('action');
|
65
|
+
|
66
|
+
jsNamespace.init(controllerName, actionName)
|
67
|
+
|
68
|
+
});
|
69
|
+
|
70
|
+
win.JsSpace = jsNamespace;
|
71
|
+
|
72
|
+
})(jQuery, this);
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'js_namespace_rails'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
module ActionView::Helpers::AssetTagHelper
|
3
|
+
|
4
|
+
alias_method :old_javascript_include_tag, :javascript_include_tag
|
5
|
+
|
6
|
+
def javascript_include_tag(*source)
|
7
|
+
|
8
|
+
origin_result = old_javascript_include_tag(*source)
|
9
|
+
|
10
|
+
_controller = controller_path.gsub(/\//,'_')
|
11
|
+
|
12
|
+
script = <<STRING
|
13
|
+
document.addEventListener('DOMContentLoaded', function() {
|
14
|
+
document.getElementsByTagName('body')[0].setAttribute('data-controller', '#{_controller}');
|
15
|
+
document.getElementsByTagName('body')[0].setAttribute('data-action', '#{action_name}');
|
16
|
+
});
|
17
|
+
STRING
|
18
|
+
origin_result + concat(javascript_tag(script, defer: 'defer'))
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: js-namespace-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Hou
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jquery-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sprockets-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
description: Namespace and Selectively Execute Javascript
|
84
|
+
email:
|
85
|
+
- hjj1992@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- README.md
|
91
|
+
- app/assets/javascripts/js-namespace-rails.js
|
92
|
+
- lib/js-namespace-rails.rb
|
93
|
+
- lib/js_namespace_rails.rb
|
94
|
+
- lib/js_namespace_rails/action_view/helpers/asset_tag_helper.rb
|
95
|
+
- lib/js_namespace_rails/engine.rb
|
96
|
+
- lib/js_namespace_rails/version.rb
|
97
|
+
homepage: https://github.com/falm/js-namespace-rails
|
98
|
+
licenses: []
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.4.3
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: js-namespace-rails let you choose which javascript snippet can execute in
|
120
|
+
rails assets pipeline
|
121
|
+
test_files: []
|