3

我正在使用backbone.js Collection fetch 方法发送请求以根据偏移量和限制返回数据。第一个 collection.fetch() 方法返回 5 个模型,因为 LIMIT 0 和 LIMIT 5 ,并且在再次调用 Backbone.Collection.prototype.fetch.call(this, options) 它返回相同的数据(以前的模型)但服务器响应下一个 OFFSET= 5 和 LIMIT = 5,即下一组对象。但我想在集合对象调用 fetch 方法时追加。

define([
  'underscore',
  'backbone',
  'utils',
  'core/base/models-and-collections',
  'core/constants'
], function(_, Backbone, Utils, CollectionUtils, Constants) {

    var Collection = Backbone.Collection.extend({
        
        initialize: function(models, options) {
            this.options = options;
            _.bindAll(this, 'parse', 'url', 'pageInfo', 'nextPage');
            typeof(options) != 'undefined' || (options = {});
            typeof(this.limit) != 'undefined' || (this.limit = 5);
            typeof(this.offset) != 'undefined' || (this.offset = 0);
            typeof(this.size) != 'undefined' || (this.size = 5);
            console.log("photo-collection-intitalize");
        },

        url: function() {
            console.log("photo-collection-url-hit");
             // if (this.size === this.limit) {            
                return Utils.keywordFormat("/api/student/{studentId}?limit={limit}&offset={offset}", {
                    studentId: this.options.studentId,
                    limit: this.limit,
                    offset: this.offset
                });           
        },
    
        nextFetch: function(options) {
            console.log("next fetch method");
          typeof(options) != 'undefined' || (options = {});
           var self = this;
          var success = options.success;
          options.success = function(resp) {
            if(success) {
                success(self, resp);
                console.info("-collection response on success");
                console.info(resp);
                }
          };
          return Backbone.Collection.prototype.fetch.call(this, options);
           
        },
    
        pageInfo: function(){

        },

        nextPage: function(){

        },

        parse: function(resp) {
            console.info(resp);
            if(!resp.items) {
                console.error("items not specified", resp);
            }
            console.log("resp:limit "+resp.limit+ "resp: offset "+ resp.offset);
            this.size= resp.limit;
            return resp.items;
        },

    });

    return Collection;

});

this.collection= new Collection();
this.collection.once("sync",this.GetFirstSetOfData,this);
this.collection.fetch();


GetFirstSetOfData: function(collection){
  
  //set view  
  
}

//set next data offset
this.collection.offset=this.collection.offset+this.collection.limit;
//call again fetch method
var newCollection=this.collection.nextFetch();

//new Collection is same data as previous this.collection have but server response next set of data 
// i,e row 5-9

4

1 回答 1

1

我会做这样的事情,当获取成功时它会触发一个“同步”事件。因此,每次成功时,您都可以增加偏移计数器:

offsetStep: 5,

initialize: function() {
    this.on('sync', this.incrementOffset);
}

incrementOffset: function() {
    this.offset += this.offsetStep;
}

然后,nextFetch您只需{add: true}在获取时传递方法,而不是使用方法 - 只要它们具有唯一的 id,这将从服务器合并到新模型中:

myCollection.fetch({ add: true });
于 2015-07-02T10:57:04.020 回答