如何从嵌套JSON与Backbone.js的建立一个集合/型号 [英] How to build a Collection/Model from nested JSON with Backbone.js
问题描述
我relativly新Backbone.js的
I'm relativly new to Backbone.js
我有一个 JSON 像图为!
我看到在骨干关系关系的一些答案,但仍然不明白了吧!
I have a JSON like the picture shows ! I saw some Answers in relation with Backbone-relational, but still dont get the point!
我如何转换这种 JSON 以Backbone.js的集合/型号?
How can i convert this JSON to Backbone.js Collections/Models??
用code我更新,但它不工作像预期的!我看不到一个模型时,我做的:
I update with a code but it dont work like expected! i can't see an model when i do :
我的结构是:
[0]:是的模型的集合
[0] : is a collection of models
[谱号] + ... + [休息]:是的模型集合
[clefs] + ... + [Rest] : are collection of models
(谱号)=> [0] + ... + [9]:是模型(标题包含字符串,路径太)
(clefs) => [0] + ... + [9] : are Models(title contains a string, path too)
非常感谢!
修改(12年1月10日):
我的解决方案:
window.initModel = Backbone.Model.extend({
defaults: {
"title": "",
"path": ""
}
});
window.CustomCollection = Backbone.Collection.extend({
model: initModel
});
window.Init = Backbone.Model.extend({
url : function(){
return "/api/data.json"
},
parse: function(response) {
clefs = new CustomCollection();
clefs.add(response.clefs);
this.set({clefs: clefs});
.....
rests = new CustomCollection();
rests.add(response.rests);
this.set({rests: rests});
}
});
<一个href=\"http://www.deploymentzone.com/2011/08/10/backbone-js-collections-of-models-within-models/\">this帮我出去呢!
推荐答案
我在工作,所以我不能给你一个完全codeD的答案,但关键是,你可以做你的最高水平以下模式,实现嵌套模型层次结构:
I'm at work, so I cannot give you a fully coded answer, but the gist is, you can do the following in your top level models to achieve a nested model hierarchy:
var AmericasNextTopModel = Backbone.Models.extend({
initialize: function(){
this.set({
clefs: new ClefCollection(this.get('clefs')),
accidentals: new AccidentalCollection(this.get('accidentals')),
notes: new NoteCollection(this.get('notes')),
rests: new RestCollection(this.get('rests'))
});
}
});
我不使用的骨干关系,所以我不能给你关于那一个答案。
I do not use backbone-relational so I cannot give you an answer regarding that.
您进行网上乐谱检视/编辑器? :D酷我很乐意看到它时,你就大功告成了。
Are you making an online sheet music viewer/editor? :D Cool I'd love to see it when you're done.
这篇关于如何从嵌套JSON与Backbone.js的建立一个集合/型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!