Skip to content

Commit 79aff38

Browse files
committed
fix(draggable): namespace events with unique ids
Until now if you had multiple instances of Draggable in the same page, when destroying one of them, all of the events binded to other instances were also destroyed. So, we are namespacing event names with unique id’s per instance to ensure only that only the corresponding events are unbinded. The other solution is to cache references to the event handlers, but most of them are proxied or throttled which makes more mess...
1 parent 1d06af1 commit 79aff38

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/jquery.draggable.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
var $window = $(window);
3737
var dir_map = { x : 'left', y : 'top' };
3838
var isTouch = !!('ontouchstart' in window);
39-
var pointer_events = {
40-
start: 'touchstart.gridster-draggable mousedown.gridster-draggable',
41-
move: 'touchmove.gridster-draggable mousemove.gridster-draggable',
42-
end: 'touchend.gridster-draggable mouseup.gridster-draggable'
43-
};
4439

4540
var capitalize = function(str) {
4641
return str.charAt(0).toUpperCase() + str.slice(1);
4742
};
4843

44+
var idCounter = 0;
45+
var uniqId = function() {
46+
return ++idCounter + '';
47+
}
48+
4949
/**
5050
* Basic drag implementation for DOM elements inside a container.
5151
* Provide start/stop/drag callbacks.
@@ -82,6 +82,8 @@
8282
this.$dragitems = $(this.options.items, this.$container);
8383
this.is_dragging = false;
8484
this.player_min_left = 0 + this.options.offset_left;
85+
this.id = uniqId();
86+
this.ns = '.gridster-draggable-' + this.id;
8587
this.init();
8688
}
8789

@@ -96,21 +98,31 @@
9698
this.disabled = false;
9799
this.events();
98100

99-
$(window).bind('resize.gridster-draggable',
101+
$(window).bind(this.nsEvent('resize'),
100102
throttle($.proxy(this.calculate_dimensions, this), 200));
101103
};
102104

105+
fn.nsEvent = function(ev) {
106+
return (ev || '') + this.ns;
107+
};
108+
103109
fn.events = function() {
104-
this.$container.on('selectstart.gridster-draggable',
110+
this.pointer_events = {
111+
start: this.nsEvent('touchstart') + ' ' + this.nsEvent('mousedown'),
112+
move: this.nsEvent('touchmove') + ' ' + this.nsEvent('mousemove'),
113+
end: this.nsEvent('touchend') + ' ' + this.nsEvent('mouseup'),
114+
};
115+
116+
this.$container.on(this.nsEvent('selectstart'),
105117
$.proxy(this.on_select_start, this));
106118

107-
this.$container.on(pointer_events.start, this.options.items,
119+
this.$container.on(this.pointer_events.start, this.options.items,
108120
$.proxy(this.drag_handler, this));
109121

110-
this.$document.on(pointer_events.end, $.proxy(function(e) {
122+
this.$document.on(this.pointer_events.end, $.proxy(function(e) {
111123
this.is_dragging = false;
112124
if (this.disabled) { return; }
113-
this.$document.off(pointer_events.move);
125+
this.$document.off(this.pointer_events.move);
114126
if (this.drag_start) {
115127
this.on_dragstop(e);
116128
}
@@ -265,7 +277,7 @@
265277
this.mouse_init_pos = this.get_mouse_pos(e);
266278
this.offsetY = this.mouse_init_pos.top - this.el_init_pos.top;
267279

268-
this.$document.on(pointer_events.move, function(mme) {
280+
this.$document.on(this.pointer_events.move, function(mme) {
269281
var mouse_actual_pos = self.get_mouse_pos(mme);
270282
var diff_x = Math.abs(
271283
mouse_actual_pos.left - self.mouse_init_pos.left);
@@ -391,9 +403,9 @@
391403
fn.destroy = function() {
392404
this.disable();
393405

394-
this.$container.off('.gridster-draggable');
395-
this.$document.off('.gridster-draggable');
396-
$(window).off('.gridster-draggable');
406+
this.$container.off(this.ns);
407+
this.$document.off(this.ns);
408+
$(window).off(this.ns);
397409

398410
$.removeData(this.$container, 'drag');
399411
};

0 commit comments

Comments
 (0)