2015-05-06 66 views
1

我有一個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" }); 

的問題是,事件不點火,那麼,什麼是錯的這個代碼?

+1

那麼,你的問題是你還沒有創建任何事件處理程序 - 只是一個函數列表。你認爲你在哪些方面將這些功能與事件綁定? –

+0

this.event = this.getEvent();在這裏添加@Morslamina –

回答

2

您可以直接define your events hash as a function

事件屬性也可以定義爲返回 事件哈希函數,以使其更容易編程的方式定義您的活動, 以及從父繼承他們觀點。

例如,

SearchView = Backbone.View.extend({ 
    process : "defaultHandler", 
    events: function() { 
     return {"click #search": this.process}; 
    }, 

    initialize: function (options) { 
     this.process = options.process; 
    },      

    searchHandler1: function (event) { 
     console.log('calling Handler1');    
    }, 
    searchHandler2: function (event) { 
     console.log('calling Handler2');    
    } 
}); 

和演示http://jsfiddle.net/rzm21q0g/

1

您的事件對象被稱爲event - 它應該是events。看看是否有幫助。

至於你的第二個問題(下面的評論)你設置this.options = options.process它應該是this.process = options.process

+0

謝謝@Morslamina這是錯字錯誤。它的幫助很大,但不是調用searchHandler1調用defaultHandler。 var myView = new SearchView({vent:mainEvent,process:「searchHandler1」});我正在通過SearchHandler1 –

+0

@MaxLuwang回答更新過程。 –

+0

值得注意的是,@nikoshr也有一個很好的答案,這可能是最好的做法 - 這樣做 - 錯別字。 –

0

有什麼不對具有在processName開關塊一個單一的事件處理程序?試試這個:

SearchView = Backbone.View.extend({ 
    el: wrapper,   
    process : "defaultHandler", 
    events: { 
     'click #search': 'onSearchClick' 
    },    
    template: _.template("<div id='cpntainer'><input type='button' id='search' value='Click Here'/><div>"),  
    initialize: function(options){ 
     this.render();   
     this.vent = options.vent; 
     this.process = options.process   
    }, 
    onSearchClick: function(event){ 
     switch(this.process){ 
      case 'searchHandler1': 
       alert('calling Handler1'); 
      break; 
      case 'searchHandler2': 
       alert('calling Handler2'); 
      break; 
      case 'searchHandler3': 
       alert('calling Handler3'); 
      break; 
      default: 
       alert('calling Deafault Handler'); 
      break; 
     } 
    }, 
    render: function() { 
     this.$el.html(this.template());   
     return this; 
    }  
}); 
var mainEvent = _.extend({}, Backbone.Events); 
var myView = new SearchView({ vent: mainEvent, process: "searchHandler1" }); 
相關問題