[英]Backbone reset a collection on fetch
這是我的問題:
這就是我設置模型/集合/視圖的方式
var someModel = Backbone.Model.extend({});
var someCollection = Backbone.Collection.extend({
model: someModel,
url: "api/someapi"
});
var someView = Backbone.View.extend({
events: {
"click #refresh": "refreshCollection"
},
initialize: function () {
this.collection.bind("reset", this.render, this);
},
render: function () {
// render stuff
},
refreshCollection: function (e) {
this.collection.fetch({data: {someParam: someValue});
this.render();
}
});
var app = function (models) {
this.start = function () {
this.models = new someCollection();
this.view = new someView({collection: this.models});
this.view.reset(models);
};
};
我感興趣的是:
refreshCollection: function (e) {
this.collection.fetch({data: {someParam: someValue});
this.render();
}
我傳入一些參數,我的api返回一個json數組模型。 我想擺脫集合中的所有現有模型,並將我所有返回的模型放入集合中,然后更新視圖(使用render())
我理解這可以通過collection.set或collection.reset實現。 這兩個都采用了一系列模型。 我沒有傳遞的模型數組。
我試過了:
this.collection.fetch({
data: {someParam: someValue},
success: function (response) {
doSomethingWith(response.models)
}
});
但是當我得到它們時,我不知道如何處理這些模型。
任何推向正確的方向將不勝感激!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.