pra*_*432 2 collections models coffeescript backbone.js
这是Backbone和下划线js的新手.
我有一组数组,我想转换为模型集合.
所以它就像
{ {1, 2, 3, 4}, {5, 6, 7, 8}}
Run Code Online (Sandbox Code Playgroud)
第二级数组是进入骨干模型的.现在,我有
collection.reset(_.map(results, (indvidualResults) -> new model(individualResults))
Run Code Online (Sandbox Code Playgroud)
哪个不起作用,当我做一个console.log(collection.pop)我得到一个打印出来的功能.我想这是因为我正在使用一组数组(但我可能是错的).如何将第二个数组转换为模型然后将其放入集合中?
重塑您的原始数据看起来更像:
[{ first: 1, second: 2, third: 3, fourth: 4 }, { first: 5, second: 6, third: 7, fourth: 8}]
Run Code Online (Sandbox Code Playgroud)
假设您的模型和集合定义如下:
var Model = Backbone.Model.extend({});
var Collection = Backbone.Collection.extend({
model: Model
});
Run Code Online (Sandbox Code Playgroud)
然后只需将属性哈希数组传递给reset方法:
var results = [{ first: 1, second: 2, third: 3, fourth: 4 }, { first: 5, second: 6, third: 7, fourth: 8}];
var collection = new Collection();
collection.reset(results);
var model = collection.pop();
console.log(JSON.stringify(model.toJSON());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11144 次 |
最近记录: |