0

爲什麼在集合中使用模型,有人可以解釋我嗎?骨幹爲什麼在集合中使用模型

例子:

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

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

可能的重複[Backbone.js集合的目的與模型](http://stackoverflow.com/questions/27448398/backbone-js-purpose-of-a-collection-with-a-模型) –

回答

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

所以每當你的項目添加到集合,可以實例化爲那種模式。