Skip to content

Commit 36b6ce8

Browse files
committed
Remove jQuery dependency. Move the eventHandler removal into Base and make some syntax changes to the generator templates.
1 parent f9dfc46 commit 36b6ce8

File tree

7 files changed

+37
-40
lines changed

7 files changed

+37
-40
lines changed

include_rails_script.js.coffee

+2-33
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
11
window.Utility || (window.Utility = {});
22
Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};
33

4-
jQuery(function() {
4+
function() {
55
window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
66
if (typeof $this.#{ action_name } === 'function') {
77
return $this.#{ action_name }.call();
88
}
9-
});
10-
11-
jQuery(document).on('page:before-change', function() {
12-
var element, handler, handlers, type, _i, _len, _ref, _results;
13-
_ref = [window, document];
14-
_results = [];
15-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
16-
element = _ref[_i];
17-
_results.push((function() {
18-
var _ref2, _results2;
19-
_ref2 = jQuery._data(element, 'events');
20-
_results2 = [];
21-
for (type in _ref2) {
22-
handlers = _ref2[type];
23-
_results2.push((function() {
24-
var _j, _len2, _results3;
25-
_results3 = [];
26-
for (_j = 0, _len2 = handlers.length; _j < _len2; _j++) {
27-
handler = handlers[_j];
28-
if (handler == null) {
29-
continue;
30-
}
31-
_results3.push(handler.namespace === '' ? $(element).off(type, handler.handler) : void 0);
32-
}
33-
return _results3;
34-
})());
35-
}
36-
return _results2;
37-
})());
38-
}
39-
return _results;
40-
});
9+
};

lib/generators/rails_script/class/templates/javascript.js.coffee

-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ window.App ||= {}
22
class App.<%= class_name.gsub('::', '') %>
33

44
constructor: ->
5-
super
65
return this

lib/generators/rails_script/element/templates/javascript.js.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ window.Element ||= {}
22
class Element.<%= element_name.gsub('::', '') %> <%= "extends Utility.#{utility.gsub('::', '')}" unless utility.blank? %>
33

44
constructor: ->
5-
super
5+
<%= "super" unless utility.blank? %>
66
return this

lib/generators/rails_script/install/templates/base.js.coffee

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,36 @@ window.App ||= {}
22
class App.Base
33

44
constructor: ->
5+
if (window.jQuery) then @setClearEventHandlers() # clearing application event handlers only possible with jQuery
56
return this
67

78

9+
###
10+
Run the new action for the create action. Generally the create action will 'render :new' if there was a problem.
11+
This prevents doubling the code for each action.
12+
###
813
create: ->
914
if typeof $this.new == 'function'
1015
return $this.new()
1116

1217

18+
###
19+
Run the edit action for the update action. Generally the update action will 'render :edit' if there was a problem.
20+
This prevents doubling the code for each action.
21+
###
1322
update: ->
1423
if typeof $this.edit == 'function'
15-
return $this.edit()
24+
return $this.edit()
25+
26+
27+
###
28+
Clear event handlers with a blank namespace. This will prevent window and document event handlers from stacking
29+
when each new page is fetched. Adding a namespace to your events will prevent them from being removed between page
30+
loads, i.e. "$(window).on 'scroll.app', myHandler"
31+
###
32+
clearEventHandlers: ->
33+
jQuery(document).on 'page:before-change', ->
34+
for element in [window, document]
35+
for event, handlers in jQuery._data(element, 'events')
36+
for handler in handlers
37+
if handler.namespace == '' then $(element).off event, handler.handler

lib/generators/rails_script/utility/templates/javascript.js.coffee

-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ window.Utility ||= {}
22
class Utility.<%= utility_name.gsub('::', '') %>
33

44
constructor: ->
5-
super
65
return this

lib/rails_script/loader_helper.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ module LoaderHelper
33

44
def include_rails_script
55
javascript_tag <<-RUBY
6-
window.Utility || (window.Utility = {});Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};jQuery(function(){window.$this=new(App.#{controller_path.split(/\/|_/).map(&:capitalize).join('')}||App.Base);if(typeof $this.#{action_name}==="function"){return $this.#{action_name}.call()}});jQuery(document).on("page:before-change",function(){var e,t,n,r,i,s,o,u;o=[window,document];u=[];for(i=0,s=o.length;i<s;i++){e=o[i];u.push(function(){var i,s;i=jQuery._data(e,"events");s=[];for(r in i){n=i[r];s.push(function(){var i,s,o;o=[];for(i=0,s=n.length;i<s;i++){t=n[i];if(t==null){continue}o.push(t.namespace===""?$(e).off(r,t.handler):void 0)}return o}())}return s}())}return u})
7-
RUBY
6+
window.Utility || (window.Utility = {});
7+
Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};
8+
9+
(function() {
10+
window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
11+
if (typeof $this.#{ action_name } === 'function') {
12+
return $this.#{ action_name }.call();
13+
}
14+
})();
15+
RUBY
816
end
917

1018
end

lib/rails_script/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RailsScript
2-
VERSION = '0.2.0'
2+
VERSION = '0.2.1'
33
end

0 commit comments

Comments
 (0)