这是backbone.js 中的正常现象吗?

发布于 2024-12-07 20:53:46 字数 379 浏览 0 评论 0原文

我似乎在渲染视图时写了这样的东西......

_.each @model.nestedcollection.models, (nestedmodel) ->

而且我觉得在某些时候我可能需要写一些......

_.each @model.nestedcollection.models, (nestedmodel) ->
    _.each nestedmodel.nestedcollection.models, (nestednestedmodel) ->
        #pass into new view

有时。

这是正常现象还是我应该以更好的方式处理这个问题?

I seem to be writing something like this when rendering views...

_.each @model.nestedcollection.models, (nestedmodel) ->

and I feel at some point i'll probably need to write something like...

_.each @model.nestedcollection.models, (nestedmodel) ->
    _.each nestedmodel.nestedcollection.models, (nestednestedmodel) ->
        #pass into new view

at times.

Is this normal or should I be dealing with this in a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我很坚强 2024-12-14 20:53:46

这是正常的。主干文档提供了用于迭代的下划线方法列表。您将在列表顶部看到 _.each。您应该了解这些方法(如果您还没有)并使用最合适的一种。您的第二个示例可能是使用 _.each 作为外部迭代器和更具体类型的迭代器(如 select、detect、reject 或 reduce)作为内部迭代器的良好用例。根据您的意图选择适当的迭代器函数。

This is normal. The backbone docs provide a list of underscore methods for iteration. You will see _.each at the top of the list. You should get to know these methods (if you haven't already) and use the most appropriate one. Your second example may be a good use case for using _.each as your outer iterator and a more specific type of iterator (like select, detect, reject, or reduce) as your inner iterator. Choose the appropriate iterator function depending on your intention.

清风挽心 2024-12-14 20:53:46

除了已经说过的之外,需要注意的一件事是主干文档中列出的功能可以直接在主干集合上使用。

您不必调用 _.each @model.nestedcollection.models, (model) ->,您可以调用 @model.nestedcollection.each (model) -> ; ...或者任何正确的咖啡脚本语法。

功能上是相同的,因为它委托给 underscore 的 each 方法,但由于它直接在集合上,因此更容易键入和阅读。

one thing to note, other than what's already been said, is that the functions listed in the backbone documentation are directly available on the backbone collections.

you don't have to call _.each @model.nestedcollection.models, (model) ->, you can instead, call @model.nestedcollection.each (model) -> ... or whatever the right coffeescript syntax for that, is.

functionally the same, since it delegates to underscore's each method, but a little easier to type and read since it's directly on the collection.

坚持沉默 2024-12-14 20:53:46

更新
我更喜欢德里克(Derick)处理你的问题的具体答案。我的建议太模糊了。

除了查看文档之外,我不熟悉主干。

有点面向对象编程:
您可以创建一个对象,自动迭代其组成的内部模型并渲染它们。这样,它们的任何组合都会自动迭代其内部模型并渲染它们。

排序功能:
或者,您可以将渲染函数传递给所有嵌套模型,并让它们各自使用它。

update
I prefer Derick's specific answer for handling your question. My suggestion is too vague.

I'm not familiar with backbone beyond reviewing the docs.

sorta OOP:
You could create an object that automatically iterates over it's composed internal models and renders them. This way any combination of them would automatically iterate over their internal models and render them.

sorta Functional:
Alternatively you could pass in a rendering function to all your nested models and have them each use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文