我只是在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