我有一個Backbone視圖(SearchView),它有幾個搜索處理程序。搜索處理程序函數必須基於某種進程類型作爲字符串動態傳遞。如何傳遞Backbone事件處理函數作爲字符串?
這裏是我的代碼
SearchView = Backbone.View.extend({
el: wrapper,
process : "defaultHandler",
event: {},
template: _.template("<div id='cpntainer'><input type='button' id='search' value='Click Here'/><div>"),
initialize: function(options){
this.render();
this.vent = options.vent;
this.options = options.process
this.event = this.getEvent();
},
getEvent : function(){
return {"click #search": this.process};
},
searchHandler1: function(event){
alert('calling Handler1');
},
searchHandler2: function(event){
alert('calling Handler1');
},
searchHandler3: function(event){
alert('calling Handler1');
},
defaultHandler: function(event){
alert('calling Deafault Handler');
},
render: function() {
this.$el.html(this.template());
return this;
}
});
var mainEvent = _.extend({}, Backbone.Events);
var myView = new SearchView({ vent: mainEvent, process: "searchHandler1" });
的問題是,事件不點火,那麼,什麼是錯的這個代碼?
那麼,你的問題是你還沒有創建任何事件處理程序 - 只是一個函數列表。你認爲你在哪些方面將這些功能與事件綁定? –
this.event = this.getEvent();在這裏添加@Morslamina –