为什么在集合中使用模型,有人可以解释一下吗?
例子:
var model = Backbone.Model.extend({
url: '/rest/article/'
});
var collection = Backbone.Collection.extend({
url: '/rest/article',
model: model
});
为什么在集合中使用模型,有人可以解释一下吗?
例子:
var model = Backbone.Model.extend({
url: '/rest/article/'
});
var collection = Backbone.Collection.extend({
url: '/rest/article',
model: model
});
因此,每当您将项目添加到集合中时,它都可以被实例化为那种模型。
var model = Backbone.Model.extend({
parse : function (response) {
// you can modify model
response.author || (response.author = 'Anonymous');
return response;
},
getArticleTitle : function () {
return this.get('author') + ' - ' + this.get('title');
}
});
var collection = Backbone.Collection.extend({
url: '/rest/article',
model: model
});
// you can access model methods
collection.at(0).getArticleTitle();