我只是在Backbone身上弄湿了自己的脚,并且我认为我有一个很容易解决的问题。我有以下看法这是一个简单的标签,当点击打开了一个面板和关闭时可以追溯到一个标签:除了“closePanel”被多次调用的骨干事件
myApp.views.Support = {
Form: Backbone.View.extend({
initialize: function() {
this.el = $('#support');
this._ensureElement();
},
render: function() {
if (this.$el.hasClass('support-panel')) {
// close panel
this.$el.empty();
this.$el.removeClass('support-panel');
this.$el.addClass('support-button');
}
else {
// open and populate panel
var template = _.template(myApp.utils.RenderTemplate('support/default'), {});
this.$el.removeClass('support-button');
this.$el.addClass('support-panel');
this.$el.html(template);
}
return this;
},
closePanel: function() {
alert('close event fired');
},
events: {
'click #SubmitFormButton': 'submitForm',
'click #CloseSupportPanel': 'closePanel'
},
submitForm: function (event) {
alert('form submitted: ' + $('#message'));
}
})
}
一切正常+2次每次被炒鱿鱼点击事件发生。我认为这是我失踪的某种清理,但我不知道是什么。
这工作,但似乎不是“最好”的方式来做到这一点。我完全承认,我绝对没有这种说法的基础,也许这是处理这种情况的正确方法。你怎么看? – Matt 2013-03-11 00:52:15
它是正确的做法。这就是JavaScript的工作方式。 – 2013-03-11 01:13:08