javascript - Backbone Paginator Collection 在重置时不触发渲染

标签 javascript jquery backbone.js

我有一个主干分页器集合:

    var ExercisesCollection = Backbone.Paginator.requestPager.extend({

        model: ExerciseModel,

        paginator_core: {
            url: "/exercises"
        },

        paginator_ui: {
            firstPage: 1,
            currentPage: 1,
            perPage: 10,
            totalPages: 10
        },

        server_api: {
            "filter": "",
            "categories": "",
            "top": function() { return this.perPage; },
            "skip": function() { return this.currentPage * this.perPage; }
        },

        parse: function (response) {
            this.totalPages = response.meta.totalPages;
            return response.data;
        }

    });

我像这样在主干 View 中使用它:

var Exercises = Backbone.View.extend({

    initialize: function () {
        this.collection = new ExercisesCollection();
        this.collection
            .on( "reset", this.render, this )
            .on( "change", this.render, this);
        this.collection.goTo( 1 );
    },

    render: function() {
        alert("bah!");
    }
});

通过查看网络事件,我可以看到它正在向服务器发送请求并接收适当的响应。但是它从不调用渲染函数。重置和更改事件似乎不起作用。

知道我做错了什么吗?

谢谢

最佳答案

我在 2016 年遇到了同样的问题:)
当您使用'server' 模式时,您需要监听'sync' 事件,而不是'reset'

// in a view
this.listenTo(this.collection, 'sync', this.renderPage);

来自 Backbone 文档(http://backbonejs.org/#Collection-fetch):

When the model data returns from the server, it uses set to (intelligently) merge the fetched models, unless you pass {reset: true}, in which case the collection will be (efficiently) reset.

因此默认情况下它不调用 reset(),因此没有 'reset' 事件。

我在这个答案中发现了 'sync' 事件: https://stackoverflow.com/a/17053978/1657101

As of backbone 1.0, model.fetch() triggers a 'sync'. That's what you should bind to.

关于javascript - Backbone Paginator Collection 在重置时不触发渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14676965/

相关文章:

javascript - CSS 光标指针覆盖元素列表拖动上的内联样式光标

javascript - Backbone.js 事件触发器

Backbone.js 多次触发 Collection 更改事件

javascript - 将指向 SVG 的链接转换为内联 SVG 元素

javascript - 如何动态分配 Angular 工厂方法?

javascript - 动态创建的按钮,需要在foreach循环中给onclick函数传入不同的值

php - WordPress中的ajax不调用php函数

jquery - 如何在 NetBeans IDE 中实现 Jquery UI Themeroller

javascript - backbone.js 中的多个主题标签路由

javascript - 在 Objective-C 中创建 Javascript 函数