Backbone.js无法捕获触发事件

时间:2017-01-22 19:40:25

标签: javascript backbone.js backbone-events eventtrigger

Hy,所以我试图抓住一个触发事件,但看起来我错过了一些东西:

所以我有骨干视图,两个平面旧的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,也许我错过了什么。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好的,所以最后我想通了,问题是订单,事件是在我宣布我的功能之前注册的,这是正确的顺序:

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

    this.view = options.view;

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