1

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

    });

私はバックボーン ビューで次のように使用します。

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

ネットワーク アクティビティを見ると、要求がサーバーに送信され、適切な応答が受信されていることがわかります。ただし、レンダリング関数を呼び出すことはありません。リセット イベントと変更イベントが機能していないようです。

私が間違っていることはありますか?

ありがとう

4

1 に答える 1

2

私は2016年に同じ問題を抱えていました:)モード
を使用しているときは、ではなくイベント'server'をリッスンする必要があります。 'sync''reset'

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

バックボーン ドキュメント ( http://backbonejs.org/#Collection-fetch ) から:

モデル データがサーバーから返されると、{reset: true} を渡さない限り、取得したモデルを (インテリジェントに) マージするために set を使用します。この場合、コレクションは (効率的に) リセットされます。

reset()したがって、デフォルトでは呼び出されないため、'reset'イベントはありません。

'sync'この回答でイベント について知りました: https://stackoverflow.com/a/17053978/1657101

バックボーン 1.0 以降、model.fetch() は「同期」をトリガーします。それがあなたがバインドする必要があるものです。

于 2016-03-18T19:51:20.417 に答える