如何使用 Backbone.js 从嵌套 JSON 构建集合/模型
我对 Backbone.js 比较陌生,
我有一个如图所示的 JSON ! 我看到了一些与骨干关系相关的答案,但仍然不明白!
我如何将此 JSON 转换为 Backbone.js 集合/模型?
我用代码更新,但它没有按预期工作!当我这样做时,我看不到模型:
我的结构是:
[0]:是模型的集合
[谱号] + ... + [其余]:是模型的集合
(谱号)=> [0] + ... + [9] :是模型(标题包含字符串,也包含路径)
非常感谢!
编辑(10.01.12):
我的解决方案:
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});
}
});
这也帮助了我!
I'm relativly new to Backbone.js
I have a JSON like the picture shows !
I saw some Answers in relation with Backbone-relational, but still dont get the point!
How can i convert this JSON to Backbone.js Collections/Models??
I update with a code but it dont work like expected! i can't see an model when i do :
My Structure is :
[0] : is a collection of models
[clefs] + ... + [Rest] : are collection of models
(clefs) => [0] + ... + [9] : are Models(title contains a string, path too)
Thanks a lot!!
EDIT(10.01.12) :
My Solution :
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});
}
});
this helped me out too!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在工作,所以我不能给你一个完整的编码答案,但要点是,你可以在你的顶级模型中执行以下操作来实现嵌套模型层次结构:
我不使用骨干关系,所以我不能给你对此的回答。
您正在制作在线乐谱查看器/编辑器吗? :D 太酷了,我很乐意在你完成后看到它。
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:
I do not use backbone-relational so I cannot give you an answer regarding that.
Are you making an online sheet music viewer/editor? :D Cool I'd love to see it when you're done.
reset
方法(参见 'reset')允许将 JSON 数组传递给集合。这相当于 PUT 方法,用 JSON 哈希替换指定的集合。
您还可以使用
add
方法添加到现有集合,或者在创建新集合时将 JSON 哈希传递到构造函数中。您必须对数组进行一些基本清理,以使其采用适当的格式,然后 将其转换为 JSON
The
reset
method (see 'reset') allows you to pass a JSON array to a collection.This is the equivalent of a PUT method, replacing the specified collection with the JSON hash.
You can also use the
add
method to add to an existing collection, or pass the JSON hash into the constructor as you create a new collection.You'll have to do some basic cleaning up of your array to get it in an appropriate format, and then convert it to JSON
我正在使用 PHP 来获取 JSON 格式的提要,因为它位于不同的域中。我将这些结果保存到一个 JS 变量中,然后我成功地使用它将其放入我的 Backbone 集合中......
I'm using PHP to grab a feed as JSON since it's on a different domain. I save those results to a JS variable, and then I just had success using this to get it into my Backbone collection...