namespaced events instead of caching callbacks

This commit is contained in:
vieron
2013-05-01 18:51:45 +02:00
parent 213a40c7ea
commit 2aa9b55433
2 changed files with 19 additions and 26 deletions

View File

@@ -24,9 +24,9 @@
var $window = $(window);
var isTouch = !!('ontouchstart' in window);
var pointer_events = {
start: isTouch ? 'touchstart' : 'mousedown.draggable',
move: isTouch ? 'touchmove' : 'mousemove.draggable',
end: isTouch ? 'touchend' : 'mouseup.draggable'
start: isTouch ? 'touchstart.gridster-draggable' : 'mousedown.gridster-draggable',
move: isTouch ? 'touchmove.gridster-draggable' : 'mousemove.gridster-draggable',
end: isTouch ? 'touchend.gridster-draggable' : 'mouseup.gridster-draggable'
};
/**
@@ -73,26 +73,25 @@
this.disabled = false;
this.events();
this.on_window_resize = throttle($.proxy(this.calculate_positions, this), 200);
$(window).bind('resize', this.on_window_resize);
$(window).bind('resize.gridster-draggable',
throttle($.proxy(this.calculate_positions, this), 200));
};
fn.events = function() {
this.proxied_on_select_start = $.proxy(this.on_select_start, this);
this.$container.on('selectstart', this.proxied_on_select_start);
this.$container.on('selectstart.gridster-draggable',
$.proxy(this.on_select_start, this));
this.proxied_drag_handler = $.proxy(this.drag_handler, this);
this.$container.on(pointer_events.start, this.options.items, this.proxied_drag_handler);
this.$container.on(pointer_events.start, this.options.items,
$.proxy(this.drag_handler, this));
this.proxied_pointer_events_end = $.proxy(function(e) {
this.$body.on(pointer_events.end, $.proxy(function(e) {
this.is_dragging = false;
if (this.disabled) { return; }
this.$body.off(pointer_events.move);
if (this.drag_start) {
this.on_dragstop(e);
}
}, this);
this.$body.on(pointer_events.end, this.proxied_pointer_events_end);
}, this));
};
fn.get_actual_pos = function($el) {
@@ -199,7 +198,7 @@
this.mouse_init_pos = this.get_mouse_pos(e);
this.offsetY = this.mouse_init_pos.top - this.el_init_pos.top;
this.on_pointer_events_move = function(mme){
this.$body.on(pointer_events.move, function(mme){
var mouse_actual_pos = self.get_mouse_pos(mme);
var diff_x = Math.abs(
mouse_actual_pos.left - self.mouse_init_pos.left);
@@ -222,9 +221,7 @@
}
return false;
};
this.$body.on(pointer_events.move, this.on_pointer_events_move);
});
return false;
};
@@ -331,11 +328,9 @@
fn.destroy = function(){
this.disable();
this.$container.off('selectstart', this.proxied_on_select_start);
this.$container.off(pointer_events.start, this.proxied_drag_handler);
this.$body.off(pointer_events.end, this.proxied_pointer_events_end);
this.$body.off(pointer_events.move, this.on_pointer_events_move);
$(window).unbind('resize', this.on_window_resize);
this.$container.off('.gridster-draggable');
this.$body.off('.gridster-draggable');
$(window).off('.gridster-draggable');
$.removeData(this.$container, 'drag');
};

View File

@@ -110,9 +110,7 @@
this.$wrapper.addClass('ready');
this.draggable();
this.on_window_resize = throttle($.proxy(this.recalculate_faux_grid, this), 200);
$(window).bind('resize', this.on_window_resize);
$(window).bind('resize.gridster', throttle($.proxy(this.recalculate_faux_grid, this), 200));
};
@@ -2550,9 +2548,9 @@
*/
fn.destroy = function(){
// remove bound callback on window resize
$(window).unbind('resize', this.on_window_resize);
$(window).unbind('.gridster');
if(this.drag_api){
if (this.drag_api) {
this.drag_api.destroy();
}