Backbone中嵌套模型的最佳实践是什么

What is the best practice for nested models in Backbone?

本文关键字:是什么 最佳 嵌套 模型 Backbone      更新时间:2023-09-26

我有一个集合"Map"和两个模型"Zone和"Section"。

Zone attributes:
    zone_title: "New zone"
    width: 0
    height: 0
    order: 0
    sections: []
Section attributes:
    section_title: "New section"
    content: "Some texts"
    order: 0

我如何将这两个模型联系起来?每个区域可以有许多部分

每个区域可以在映射中进行排序,每个都可以在区域进行分类。

这样做的最佳做法是什么

我建议您看看Backbone relational——我认为它涵盖了您的用例。

既然你问了最佳实践,据我所知,在没有任何插件的情况下使用Backbone的最佳实践是模型应该总是浅层的。您可以通过获取Map集合来加载Zones集合,但这些Zones会很浅。对于每个Zone,您将单独调用以获取该Zone的Section集合。

不过,您的用例会有所不同。您是否同时加载所有分区?你想同时加载所有分区的所有分区吗?还是一次只获取一个或几个区域的分区?

    { "Zones": [ { "zone_title":"New zone 1" , "width": 0, "height" : 0, "order" : 0, "sections": [ 
{"section_title": "New section 1", "content" : "Some texts", "order": 0}, 
{"section_title": "New section 2", "content" : "Some texts", "order": 0} ] },  
{ "zone_title":"New zone 2" , "width": 0, "height" : 0, "order" : 0, "sections": [ 
{"section_title": "New section 3", "content" : "Some texts", "order": 0} ] },  
{ "zone_title":"New zone 3" , "width": 0, "height" : 0, "order" : 0, "sections": [{}] }  ] }