2017-01-22 77 views
-1

海蘭,所以我想抓住一個觸發事件,但它看起來像我想的東西:Backbone.js的不能趕上觸發事件

所以我有骨氣觀點,和兩個平面老的javascript對象,一個是「控制器」,一個是我的「應用程序」對象,它看起來像這樣:

// View 
SearchView = Backbone.View.extend({ 
    ... 

    events: { 
     'keyup .input' : 'search' 
    }, 

    search: function (e) { 

     var keyCode = e.keyCode || e.which; 

     this.trigger("search:auto", keyCode); 
    } 
}); 

// Controller 
SearchController = function (options) { 
    ... 

    this.view = options.view; 

    this.view.on("search:auto", this.search, this); 

    this.search = function() { 
     console.log('great'); 
    }; 
}; 

// App 
App = function() { 
    ... 

    this.views = { 
     searchView : new SearchView() 
    }; 

    new SearchController({ 
     view: this.views.searchView 
    }); 

    this.start = function() { 
     ... 
    }; 
} 

new App().start(); 

而問題是,我要趕在視圖觸發事件的控制器,但不知何故,它不工作,我知道Backbone,也許我錯過了一些東西。

感謝您的任何幫助。

回答

0

好了,我終於想通了,問題的順序,該事件被註冊之前,我宣佈我的功能,這是正確的順序:

// Controller 
SearchController = function (options) { 
    ... 

    this.view = options.view; 

    this.search = function() { 
     console.log('great'); 
    }; 
    this.view.on("search:auto", this.search, this); 
};