我有一个带有文本的 SVG 画布,我想响应点击。下面的代码没有完成工作:
var MyView = Backbone.View.extend({
tagName : "text",
events : {
"click" : "clickhandler"
},
initialize : function() {
this.centerX = this.options.centerX;
this.centerY = this.options.centerY;
this.svg = this.options.svg;
this.tagText = this.model.get("tag_name");
this.render();
},
clickhandler : function(event) {
console.log("I was clicked!"); //This is not firing on click
},
render : function() {
this.el = this.svg.text(this.centerX, this.centerY, this.tagText, {});
return this;
}
});
这是在另一个视图的渲染函数中调用的:
container.svg({
onLoad : function(svg) {
for ( var i = 1; i < that.relatedTags.length; i++) {
tagView = new MyView({
model : this.relatedTags.at(i),
centerX : 100,
centerY : 200,
svg : svg
});
}
container.append(tagView);
}
});
它显示得很好,如果我在 for 循环结束时把它扔进去:
$(tagView.el).click(function() {
alert("xx");
});
然后点击工作,但我需要访问与视图关联的主干模型,所以我更喜欢主干事件而不是直接的 JQuery 事件。