每个分页后数据附加到同一个集合取 [英] Appending data to same collection after every pagination fetch
问题描述
我试图用填充骨干Instagram的图片,
I am trying to populate instagram images using backbone,
我基本上有3个型号如下,
I have basically 3 models as follows,
用户模型存储中的所有用户相关的信息,以Instagram的
App.Models.User = Backbone.Model.extend({
defaults: {
id: '',
access_token: '',
userid: '',
username: '',
full_name: '',
profile_picture: ''
},
urlRoot: "/api/user/",
initurl: function() {
return "https://api.instagram.com/v1/users/"+this.get('userid')+"/media/recent/?access_token=" + this.get('access_token');
},
initialize: function() {
this.set('id', $('#domdump .id').val());
this.fetch({
success: function(model) {
var photos = new App.Collections.Ig_photos([],{
url: model.initurl()
});
}
});
}
});
以存储下一个网址为分页模型
App.Models.Ig_next_url = Backbone.Model.extend({
defaults: {
next_url: ''
},
next_url:function(){
return this.get('next_url');
}
});
:一种为照片模式
App.Models.Ig_photo = Backbone.Model.extend({});
一种多照片集
App.Collections.Ig_photos = Backbone.Collection.extend({
model: App.Models.Ig_photo,
initialize: function(model, options) {
this.url = options.url;
this.nextSet();
},
sync: sync_jsonp,
parse: function( response ) {
if(response.pagination && response.pagination.next_url && response.pagination.next_url != this.url){
var next_url = new App.Models.Ig_next_url({ next_url: response.pagination.next_url });
this.url = next_url.next_url();
}
return response.data;
},
nextSet: function(){
this.fetch({
success: function(photos){
var ig_photos_views = new App.Views.Ig_photos_view({ collection: photos});
console.log(photos);
}
});
}
});
此外,我有一些看法,它的带负载更多按钮调用集合的nextset渲染。
Also i have some views that does the render with a load more button that calls the nextset of the collection.
我试图实现的是相片会在nextset追加到集合()和收集与获取数据透水+新的数据更新,但现在它的覆盖掉了。
What i was trying to achieve is the photos get appended to the collection upon nextset() and the collection get updated with pervious data + new data but right now its getting overwritten.
也是它没关系从modelfetch实例化新的集合?
Also is it okay to instantiate new collection from the modelfetch ?
推荐答案
您不应该需要做一个新的看法。而应该听听添加事件上采集被触发,并相应地呈现新的项目。
You shouldn't need to make a new view. You should instead listen to the "add" event being triggered on the collection and render new items accordingly.
nextSet: function(){
this.fetch({add : true}); // The add option appends to the collection
}
这个选项在很好的文档。
这篇关于每个分页后数据附加到同一个集合取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!