js-namespace-rails 0.2.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: afc31bbe52d154ba9147040e7fcaaa6482ee30bc
4
- data.tar.gz: d9eef4c54e60ba656f13849e35c4e639ed8f0af1
3
+ metadata.gz: fe0b83f913fadbc40f98ce658c77de91c0bef46f
4
+ data.tar.gz: 3d3d5955da1b367240c78da70b59f93e8af4621a
5
5
  SHA512:
6
- metadata.gz: 755f35e0962a061ae08eca6b1b06d01a4eaa73f2782780b6d58791df6ab9475c3b794c267e8763366a5ad0946dd0ee5832f00ca9a8d26b5b7838c2064962a06d
7
- data.tar.gz: 1fdd49ba2e14fab4deb51587be2d7cc2d121671bb10f41f039a8db68ad7caba24cb930432f50f37043a43858e2ba008b8b04b83d822cf9dc1a167cdd86f9c75c
6
+ metadata.gz: eec5bb362b618e9c09ad71b2a62a9778ada9dd5402de75f03142e058b1149e065b72dfef5eb3eea853d94e2d9a1e34d636db5c6805f25177e352c4adec2b8d66
7
+ data.tar.gz: 89e4d5d598a1e03d00073175a50ea6cb21154d9ed79a0f6ba2d0f8de2d822d3581ef370a7664234fcfdd344c3b3cac1caa4b951860ad77ed381086b52918180f
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # JsNamespaceRails
1
+ # JsNamespaceRails [![Build Status](https://travis-ci.org/falm/js-namespace-rails.svg?branch=master)](https://travis-ci.org/falm/js-namespace-rails) [![Coverage Status](https://coveralls.io/repos/github/falm/js-namespace-rails/badge.svg?branch=master)](https://coveralls.io/github/falm/js-namespace-rails?branch=master) [![Code Climate](https://codeclimate.com/github/falm/js-namespace-rails/badges/gpa.svg)](https://codeclimate.com/github/falm/js-namespace-rails) [![Dependency Status](https://gemnasium.com/badges/github.com/falm/js-namespace-rails.svg)](https://gemnasium.com/github.com/falm/js-namespace-rails)
2
2
 
3
3
 
4
4
  Rails's asset pipeline compiles all of js file into a single file which is executed on all pages.
@@ -28,29 +28,25 @@ Require js-namespace-rails file in application.js or other bundle,notice js-name
28
28
  ## Usage
29
29
  Assume your project have articles_controller
30
30
  ``` ruby
31
- class ArticlesController < ApplicationController
32
- def index
33
- end
34
- end
31
+ class ArticlesController < ApplicationController
32
+ def index
33
+ end
34
+ end
35
35
  ```
36
36
  And its corresponding js file app/assets/javascripts/articles.js.erb
37
37
 
38
38
  then you just need to write below into the js file
39
39
  ``` js
40
- // app/assets/javascripts/articles.js.erb
41
- JsSpace.on('articles', {
42
- init: function(){
43
- console.log('common logic of article in here');
44
- },
45
- index: function(){
46
- console.log('logic of index action in here');
47
- }
48
- });
40
+ // app/assets/javascripts/articles.js.erb
41
+ JsSpace.on('articles', {
42
+ init: function(){
43
+ console.log('common logic of article in here');
44
+ },
45
+ index: function(){
46
+ console.log('logic of index action in here');
47
+ }
48
+ });
49
49
  ```
50
50
 
51
51
  ## License
52
52
  MIT License.
53
-
54
-
55
-
56
-
@@ -1,5 +1,5 @@
1
1
 
2
- (function($, win){
2
+ (function(win){
3
3
 
4
4
  'use strict';
5
5
 
@@ -9,59 +9,58 @@
9
9
 
10
10
  self.INIT = 'init';
11
11
 
12
- self.PARAMS_CONTAINER = '.executive-isolation-params';
13
-
14
- self.contrllerList = {};
12
+ self.controllerList = {};
15
13
 
16
14
  self.on = function(controllerName, obj){
17
- self.contrllerList[controllerName] = obj;
15
+ self.controllerList[controllerName] = obj;
18
16
  };
19
17
 
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
- };
18
+ self.params = {};
30
19
 
31
- self.setInit = function(controllerName, actionName){
32
- var $body = $('body');
33
- $body.attr('data-controller', controllerName);
34
- $body.attr('data-action', actionName);
20
+ self.fetchParams = function(){
21
+ return self.params;
35
22
  };
36
23
 
37
24
  self.init = function(controllerName, actionName){
38
25
 
39
- var params = [self.fetchParams()];
26
+ var params = self.fetchParams();
40
27
 
41
- var activeController = self.contrllerList[controllerName];
28
+ var activeController = self.controllerList[controllerName];
42
29
 
43
30
  if( activeController !== undefined && typeof activeController === 'object') {
44
31
 
32
+ activeController.params = params;
33
+
45
34
  if(activeController[self.INIT] !== undefined && typeof activeController[self.INIT] === 'function'){
46
35
  activeController.init(params);
47
36
  }
48
37
 
49
38
  if(activeController[actionName] !== undefined && typeof activeController[actionName] === 'function'){
50
- activeController[actionName].apply(null, params);
39
+ activeController[actionName].call(activeController, params);
51
40
  }
52
41
  }
42
+ };
43
+
44
+ self.ready = function(fn){
45
+ if ( typeof fn !== 'function' ) return;
46
+
47
+ if ( document.readyState === 'complete' ) {
48
+ return fn();
49
+ }
50
+
51
+ document.addEventListener( 'interactive', fn, false );
53
52
  }
54
53
  }
55
54
 
56
55
  var jsNamespace = new JsNamespace();
57
56
 
58
- $(document).ready(function(){
57
+ document.addEventListener('DOMContentLoaded', function(){
59
58
 
60
- var $body = $('body');
59
+ var $body = document.querySelector('body');
61
60
 
62
- var controllerName = $body.data('controller');
61
+ var controllerName = $body.getAttribute('data-controller');
63
62
 
64
- var actionName = $body.data('action');
63
+ var actionName = $body.getAttribute('data-action');
65
64
 
66
65
  jsNamespace.init(controllerName, actionName)
67
66
 
@@ -69,4 +68,4 @@
69
68
 
70
69
  win.JsSpace = jsNamespace;
71
70
 
72
- })(jQuery, this);
71
+ })(this);
@@ -0,0 +1,5 @@
1
+ document.addEventListener('DOMContentLoaded', function() {
2
+ document.getElementsByTagName('body')[0].setAttribute('data-controller', '<%= controller_path.gsub(/\//,'_') %>');
3
+ document.getElementsByTagName('body')[0].setAttribute('data-action', '<%= action_name %>');
4
+ window.JsSpace.params = <%= raw @js_namespace_rails_params.to_json %>;
5
+ });
@@ -0,0 +1,2 @@
1
+
2
+ JsSpace.init('<%= controller_path.gsub(/\//,'_')%>', '<%= action_name %>');
@@ -0,0 +1,26 @@
1
+
2
+ module JsNamespaceRails
3
+ module ActionControllerExtension
4
+
5
+ def self.included(base)
6
+ base.module_eval do
7
+ helper_method :js_execute
8
+ helper_method :insert_hook_script
9
+ end
10
+ end
11
+
12
+ def js(params)
13
+ @js_namespace_rails_params ||= {}
14
+ @js_namespace_rails_params = @js_namespace_rails_params.merge(params)
15
+ end
16
+
17
+ def js_execute
18
+ view_context.render(partial: 'js_namespace_rails/init')
19
+ end
20
+
21
+ def insert_hook_script
22
+ view_context.render(partial: 'js_namespace_rails/hook.js.erb')
23
+ end
24
+ end
25
+ ::ActionController::Base.send :include, ActionControllerExtension
26
+ end
@@ -5,22 +5,11 @@ module ActionView::Helpers::AssetTagHelper
5
5
 
6
6
  def javascript_include_tag(*source)
7
7
 
8
- origin_result = javascript_include_tag_without_controller(*source)
9
-
10
8
  if defined?(controller_path) && !@_included
11
- _controller = controller_path.gsub(/\//,'_')
12
9
  @_included = true
13
- script = <<STRING
14
- document.addEventListener('DOMContentLoaded', function() {
15
- document.getElementsByTagName('body')[0].setAttribute('data-controller', '#{_controller}');
16
- document.getElementsByTagName('body')[0].setAttribute('data-action', '#{action_name}');
17
- });
18
- STRING
19
- origin_result + concat(javascript_tag(script, defer: 'defer'))
10
+ concat javascript_tag(insert_hook_script, defer: 'defer')
20
11
  end
21
-
22
- origin_result
23
-
12
+ javascript_include_tag_without_controller(*source)
24
13
  end
25
14
 
26
- end
15
+ end
@@ -4,8 +4,6 @@ module JsNamespaceRails
4
4
 
5
5
  class Engine < ::Rails::Engine
6
6
 
7
- require 'jquery-rails'
8
- # isolate_namespace
9
7
  end
10
8
 
11
9
  end
@@ -1,3 +1,3 @@
1
1
  module JsNamespaceRails
2
- VERSION = '0.2.2'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js-namespace-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Hou
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-07 00:00:00.000000000 Z
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -17,27 +17,13 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
- type: :runtime
20
+ type: :development
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: '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
27
  - !ruby/object:Gem::Dependency
42
28
  name: sprockets-rails
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -45,7 +31,7 @@ dependencies:
45
31
  - - ">="
46
32
  - !ruby/object:Gem::Version
47
33
  version: '0'
48
- type: :runtime
34
+ type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
@@ -56,14 +42,14 @@ dependencies:
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - "~>"
45
+ - - ">="
60
46
  - !ruby/object:Gem::Version
61
47
  version: '1.8'
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - "~>"
52
+ - - ">="
67
53
  - !ruby/object:Gem::Version
68
54
  version: '1.8'
69
55
  - !ruby/object:Gem::Dependency
@@ -94,6 +80,20 @@ dependencies:
94
80
  - - ">="
95
81
  - !ruby/object:Gem::Version
96
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.7'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: capybara
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rack
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.5'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.5'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: capybara-webkit
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -131,8 +145,11 @@ extra_rdoc_files: []
131
145
  files:
132
146
  - README.md
133
147
  - app/assets/javascripts/js-namespace-rails.js
148
+ - app/views/js_namespace_rails/_hook.js.erb
149
+ - app/views/js_namespace_rails/_init.js.erb
134
150
  - lib/js-namespace-rails.rb
135
151
  - lib/js_namespace_rails.rb
152
+ - lib/js_namespace_rails/action_controller_extension.rb
136
153
  - lib/js_namespace_rails/action_view/helpers/asset_tag_helper.rb
137
154
  - lib/js_namespace_rails/engine.rb
138
155
  - lib/js_namespace_rails/version.rb
@@ -156,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
173
  version: '0'
157
174
  requirements: []
158
175
  rubyforge_project:
159
- rubygems_version: 2.4.3
176
+ rubygems_version: 2.5.1
160
177
  signing_key:
161
178
  specification_version: 4
162
179
  summary: js-namespace-rails let you choose which javascript snippet can execute in