尝试嗅探何时调用filterMe,以便更新视图:

var MyCollection = Backbone.Collection.extend({
    model: MyModel,
    url: '/myapi',
    filterMe: function() {
        return this.where({tag: "A"});
    }
});

var MyView = Backbone.View.extend({
    el: 'div',
    initialize: function() {
        var that = this;
        //Anytime I call filterMe, I need to update the view with filtered data
        this.listenTo(my_collection, 'filterMe', this.render);
        my_collection.fetch({
            success: function() {
                that.render();
            }
        });
    },
    render: function() {
        ...
    }
)};

var my_collection = new MyCollection();
var my_view = new MyView();


每当执行filterMe时都需要触发渲染功能。有任何想法吗?非常感谢!

最佳答案

试试看

filterMe: function() {
    this.trigger("filterMe");
    return this.where({tag: "A"});
}

关于javascript - 在Backbone.js View 中监听集合的函数调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21252397/

10-12 19:47