0

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

例子:

var model = Backbone.Model.extend({
  url: '/rest/article/'
});

var collection = Backbone.Collection.extend({
  url: '/rest/article',
  model: model
});
4

2 回答 2

1

因此,每当您将项目添加到集合中时,它都可以被实例化为那种模型。

于 2013-10-27T17:55:16.110 回答
1
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();
于 2013-10-27T19:59:44.447 回答