Skip to content

Commit 82cf5bf

Browse files
committed
Make sure only window/document eventListeners created by the rails_script application are destroyed during page changes. Adding a namespace to window or document events will prevent them from being destroyed.
1 parent 2454f6a commit 82cf5bf

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

include_rails_script.js

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

lib/rails_script/loader_helper.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@ module LoaderHelper
33

44
def include_rails_script
55
javascript_tag <<-RUBY
6-
jQuery(function() {
7-
window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
8-
if (typeof $this.#{ action_name } === 'function') {
9-
return $this.#{ action_name }.call();
10-
}
11-
});
12-
13-
jQuery(document).on('page:before-change', function() {
14-
jQuery(document).off();
15-
jQuery(window).off();
16-
});
6+
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})
177
RUBY
188
end
199

0 commit comments

Comments
 (0)