解析模型进出的大型 json 数据
我有一个大型 json 服务器响应,它将模型嵌套在模型中。我没有办法让服务器基于休息。我需要将 json 树解析为集合和集合内的集合。然后我想在修改后将相同 json 结构的骨干模型导出到服务器。
我有一个名为sections的集合,并使用此方法将问题集合嵌套在每个section模型中: http://documentcloud.github.com/backbone/#FAQ-nested
顶部json 响应的级别是节数组,因此我能够将其直接传递到节集合,然后使用初始化方法解析子问题,然后从属性中删除它们。但这意味着我没有在sections集合的任何toJSON中返回model.questions。
SCC.Models.Section = Backbone.Model.extend({
initialize: function() {
var questions = this.get('questions');
this.questions = new SCC.Collections.Questions(questions);
delete this.attributes.questions;
}
});
SCC.Collections.Sections = Backbone.Collection.extend({
model: SCC.Models.Section
});
SCC.Sections = new SCC.Collections.Sections();
//imagine window.SectionData is the server response
SCC.Sections.reset(window.SectionData);
希望我很清楚。如果您需要更多信息,请告诉我。
谢谢。
I have a large json server response which nests models in models. I have no way of making the server rest based. I need to parse the json tree into collections and collections within collections. I would then like to export the backbone models in the same json structure to the server after modification.
i have a collection called sections, and using this method to nest the questions collection within each section model:
http://documentcloud.github.com/backbone/#FAQ-nested
the top level of the json response is the array of sections so i have been able to pass that directly to the sections collection, I have then used the initialize method to parse out the child questions and then deleted them from the attributes. But this means i dont get the model.questions returned in any toJSON on the sections collection.
SCC.Models.Section = Backbone.Model.extend({
initialize: function() {
var questions = this.get('questions');
this.questions = new SCC.Collections.Questions(questions);
delete this.attributes.questions;
}
});
SCC.Collections.Sections = Backbone.Collection.extend({
model: SCC.Models.Section
});
SCC.Sections = new SCC.Collections.Sections();
//imagine window.SectionData is the server response
SCC.Sections.reset(window.SectionData);
Hopefully I am clear. Let me if you need more info.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您控制的嵌套模型数量有限并且不会频繁更改,那么简单的方法是为具有嵌套集合的模型覆盖
toJSON()
方法:但是,如果你有很多嵌套的集合,这些集合可能会发生变化,更抽象的方式会很好。
例如,我可以考虑将所有嵌套集合存储在模型的特殊属性中(例如
nestedCollections
),然后猴子修补Backbone.Model.prototype.toJSON
,以便它始终解析所有nestedCollections 并将它们添加到 JSON 对象,然后返回它。另一种方法是使用 Backbone 关系 而不是嵌套模型(它会自动处理 toJSON,所以你不会'不必考虑它)。
If you have a limited number of nested models which are under you control and are not subject to frequent change, the easy way is to override
toJSON()
method for your models that have nested collections:However, if you have a lot of nested collections which are subject to change, more abstract way would be nice.
I can think for example of storing all nested collections in model's special property (say
nestedCollections
) and then monkey patchingBackbone.Model.prototype.toJSON
so that it will always parse all nestedCollections and add them to JSON object before returning it.Another way would be to use Backbone relational instead of nested models (it automatically handles toJSON so you won't have to think about it).