干净地处理子对象的 JavaScript 框架?
我一直在对主干和淘汰等框架进行大量研究,并试图找出处理密集对象层次结构以及将对象参数绑定到 DOM 时的最佳选择。
我正在处理具有自己的参数但也包含对象的对象。这些对象依次具有自己的参数并包含对象等。达到大约 5-6 个对象的级别。例如:
var Program = {
id: 123,
name: "some name",
cycle1 {
cycleName: "another name",
startDate: "1-1-2012",
day1 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
day2 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
}
cycle2 {
cycleName: "another name",
startDate: "1-1-2012",
day1 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
day2 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
}
}
这是一个非常简化的示例,但给出了一个想法。这里,有一个 Program 对象将包含未知数量的 Cycle 对象,其中将包含 Day 对象,后者将包含 MetaData 对象。同样,其数量几乎肯定是完全可变的,并且每个项目都需要绑定到一个 DOM 元素。
我知道 Backbone 确实不适合嵌套……虽然 Knockout 可能也不是,但我觉得我可能可以更快地掌握它。
I've been doing a lot of research into frameworks like backbone and knockout, and am trying to figure out the best option when it comes to handling dense object hierarchies, and binding object parameters to the DOM.
I'm working with objects that have their own parameters, but that also contain objects. Those objects in turn have their own parameters and contain objects, etc. To a level of about 5-6 objects. For example:
var Program = {
id: 123,
name: "some name",
cycle1 {
cycleName: "another name",
startDate: "1-1-2012",
day1 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
day2 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
}
cycle2 {
cycleName: "another name",
startDate: "1-1-2012",
day1 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
day2 {
startDate: "1-1-2012",
metaData {
foo: "bar",
foo2: "bar 2"
}
}
}
}
This is a VERY simplified example, but gives an idea. Here, there is a Program object that will contain an unknown number of Cycle objects, which will contain Day objects, which will contain MetaData objects. Again, the numbers of which are almost certain to be completely variable, and each item will need to be bound to a DOM element.
I know Backbone really isn't set up for nesting... and while Knockout might not be either, I feel like I could probably grok it more quickly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能说 Backbone,但 Knockout 可以很好地处理嵌套对象。
例如,您可以设置一个用于迭代主对象的模板,然后在该模板内设置一个元素,在其中使用另一个模板来迭代子对象,等等。
或者您可以直接在主模板中迭代子对象,或者最适合您的用例的任何内容。
I can't say for Backbone, but Knockout handles nested objects just fine.
You can for example set up a template which is used to iterate your main object, then set up an element inside that template where you use another template to iterate the sub-object, and so on.
Or you could just iterate the sub-object directly in your main template, or whatever suits your use-case best.