可以說,我得到了這樣的觀點:。骨幹JS動態事件與變量
var HomeView = Backbone.View.extend({
el: '#application',
initialize: function() {
this.template = template; // Comes from requireJS (not relevant)
this.$elements = {};
},
render: function() {
this.$el.html(this.template);
this.$elements = {
signIn: {
email: $('#sign-in-email'),
password: $('#sign-in-password')
}
};
// Demonstration.
this.$elements.signIn.email.myPluginInit();
this.$elements.signIn.password.myPluginInit();
//
// NOTE: How to handle the events?
//
}
});
我有這個$元素對象,其中將包含我的DOM存在的所有對象,我怎樣才能把事件對他們因爲採用這種解決方案它們是可變的這是我以前做的事(參見backbone.org)。
var HomeView = Backbone.View.extend({
events: {
'click #sign-in-email': 'clickedSignInEmail',
'focus #sign-in-password': 'focusSignInPassword'
}
});
聽起來像是你應該有不同的視圖結構,你會使用不同的子視圖取決於什麼部件,你需要在頁面上。然後,您的登錄事件將綁定到一個子視圖,並且您不會有可變事件。 –