1

我有一个返回 JSON 对象数组的 Restful 服务。该服务允许用户指定最大结果和数据分页。我将此服务映射到一个集合,并将 max_result 默认设置为 10 和第 1 页。我现在想从下一页获取数据并更新/更改集合中的模型我该怎么做。骨干集合:

define(['backbone','models/Video'],function(Backbone,Video) {
return Rock = Backbone.Collection.extend ({
    model:Video,
    url:"/root/max_result/page"
});
});
4

1 回答 1

1

所以 url 看起来像'/root/10/2'第 2 页?

define(['backbone','models/Video'],function(Backbone,Video) {
  return Videos = Backbone.Collection.extend ({
    model:Video,
    initialize: function(models, options){
        // get options.genre or use 'rock' as default
        this.genre = options && _.has(options, 'genre') ? options.genre : 'rock';
    },
    fetch: function(options){
       // make sure we have options object
       options = options ? _.clone(options) : {};

       // if no url in options, create url using options.page
       if(!_.has(options, 'url')){
         options.url = "/" + this.genre + "/10/" + options && _.has(options, 'page') ? options.page : 1;
       }

       return Backbone.Collection.prototype.fetch.apply(this, [options]);
    }
  });
});

// fetch page 3
var rock = new Videos(null, {genre: 'rock'});
rock.on('reset', function(){
    // each time you fetch, this will be called.
});
rock.fetch({page:3});
于 2013-03-07T06:23:12.230 回答