骨干分页器集合在重置时不会触发渲染 [英] Backbone Paginator Collection not triggering render on Reset

查看:75
本文介绍了骨干分页器集合在重置时不会触发渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Backbone Paginator Collection:

    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;
        }

    });

我像这样在骨干网视图中使用它:

I use it in a Backbone View like so:

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!");
    }
});

通过查看网络活动,我可以看到它正在将请求发送到服务器并收到适当的响应.但是,它从不调用render函数.重置和更改事件似乎无效.

By looking at the network activity, I can see that it is sending the request to the server and receiving an appropriate response. However it never calls the render function. The reset and change events do not seem to be working.

有什么想法我在做什么错吗?

Any ideas what I'm doing wrong?

谢谢

推荐答案

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

I had the same problem in 2016 :)
When you're using the 'server' mode, you need to listen to the 'sync' event, not 'reset'.

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

从骨干文档( http://backbonejs.org/#Collection-fetch ):

当模型数据从服务器返回时,它将使用设置为 (以智能方式)合并获取的模型,除非您通过{reset: true},在这种情况下,集合将被(有效地)重置.

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'事件.

So it doesn't call reset() by default, and so no 'reset' event.

我在以下答案中发现了关于'sync'事件的信息: https://stackoverflow.com/a/17053978/1657101

I have found out about the 'sync' event in this answer: https://stackoverflow.com/a/17053978/1657101

从主干1.0开始,model.fetch()触发同步".那就是你应该绑定的东西.

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

这篇关于骨干分页器集合在重置时不会触发渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆