1
我很想解除綁定此:underscore.js取消綁定
$("body").mousemove(_.bind(this.mousemove, this));
由於Backbone.js的和raphael.js之間的複雜的混合體,我需要做通過underscore.js綁定:
var NodeView = Backbone.View.extend({
dx: 0,
dy: 0,
click: function(event){
alert('hello')
},
mousedown: function(event){
this.dx = event.pageX - this.el.attr('x');
this.dy = event.pageY - this.el.attr('y');
this.el.attr({fill: "#0099FF"});
$("body").mousemove(_.bind(this.mousemove, this));
},
mousemove: function(event){
this.el.attr({ x: event.pageX - this.dx,
y: event.pageY - this.dy});
},
mouseup: function(event){
this.el.attr({fill: "#EEEEEE"});
$("body").mousemove(_.bind(this.mousenotmove, this));
},
render: function(){
this.el = canvas.rect(this.model.get('xPos'), this.model.get('yPos'), 50, 50).attr({
fill: "#EEEEEE",
stroke: "none",
cursor: "move"
});
$(this.el.node).mousedown(_.bind(this.mousedown, this));
$(this.el.node).mouseup(_.bind(this.mouseup, this));
return this;
}
});
有什麼建議嗎?
那裏面還混合了jQuery代碼嗎? – Pointy 2011-04-08 12:59:14
此外,Underscore「綁定」在這裏成爲一個問題的可能性極小。它看起來像你需要做的是解除綁定事件處理程序,它沒有(直接)與Underscore「綁定」被用於什麼。所有它(「_.bind()」)提供的函數都帶有一個預先綁定的this值。 – Pointy 2011-04-08 13:03:22
是的,還有jQuery混合在一起。問題是我有一個Backbone視圖和一個Raphäel對象,我需要更新附加到視圖的Backbone模型。我不能這樣做,如果我將事件直接綁定到Raphäel對象... – thgie 2011-04-08 13:18:53