gpt4 book ai didi

javascript - 为什么在集合中使用模型的主干

转载 作者:行者123 更新时间:2023-12-02 18:12:08 25 4
gpt4 key购买 nike

为什么在集合中使用模型,有人可以解释一下吗?

示例:

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();

关于javascript - 为什么在集合中使用模型的主干,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com