<legend id='8GtrH'><style id='8GtrH'><dir id='8GtrH'><q id='8GtrH'></q></dir></style></legend>

    • <bdo id='8GtrH'></bdo><ul id='8GtrH'></ul>
        <tfoot id='8GtrH'></tfoot>

        <small id='8GtrH'></small><noframes id='8GtrH'>

        <i id='8GtrH'><tr id='8GtrH'><dt id='8GtrH'><q id='8GtrH'><span id='8GtrH'><b id='8GtrH'><form id='8GtrH'><ins id='8GtrH'></ins><ul id='8GtrH'></ul><sub id='8GtrH'></sub></form><legend id='8GtrH'></legend><bdo id='8GtrH'><pre id='8GtrH'><center id='8GtrH'></center></pre></bdo></b><th id='8GtrH'></th></span></q></dt></tr></i><div id='8GtrH'><tfoot id='8GtrH'></tfoot><dl id='8GtrH'><fieldset id='8GtrH'></fieldset></dl></div>

      1. 让模型听嵌套模型和集合的最佳模式?

        Best pattern to have models listen to nested models and collections?(让模型听嵌套模型和集合的最佳模式?)
          <tfoot id='yU5Pg'></tfoot>

            <legend id='yU5Pg'><style id='yU5Pg'><dir id='yU5Pg'><q id='yU5Pg'></q></dir></style></legend>
              <tbody id='yU5Pg'></tbody>

              • <small id='yU5Pg'></small><noframes id='yU5Pg'>

                  <bdo id='yU5Pg'></bdo><ul id='yU5Pg'></ul>

                • <i id='yU5Pg'><tr id='yU5Pg'><dt id='yU5Pg'><q id='yU5Pg'><span id='yU5Pg'><b id='yU5Pg'><form id='yU5Pg'><ins id='yU5Pg'></ins><ul id='yU5Pg'></ul><sub id='yU5Pg'></sub></form><legend id='yU5Pg'></legend><bdo id='yU5Pg'><pre id='yU5Pg'><center id='yU5Pg'></center></pre></bdo></b><th id='yU5Pg'></th></span></q></dt></tr></i><div id='yU5Pg'><tfoot id='yU5Pg'></tfoot><dl id='yU5Pg'><fieldset id='yU5Pg'></fieldset></dl></div>
                  本文介绍了让模型听嵌套模型和集合的最佳模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 Backbone.js 让模型一直监听所有嵌套模型和集合的最佳模式是什么?

                  Using Backbone.js what is the best pattern to have models listen to all of their nested models and collections all the way down?

                  我应该将嵌套模型/集合放在属性中吗?我应该手动创建父母关系并触发事件吗?

                  Should I put nested models/collections in attributes? Should I create parental relationships and trigger events manually?

                  推荐答案

                  与大多数 Backbone.js 一样,你不会得到正确"的答案,但我可以分享我是如何做到的.对于模型和视图,我通常遵循以下准则:

                  As with most things Backbone.js, you won't get a "right" answer to this, but I can share how I do it. With both models and views, I generally follow these guidelines:

                  • 视图和模型应该由对它们负责"的组件实例化.对于具有明确父子关系的模型或视图,父模型应该实例化,通常在 initialize() 方法中.

                  父母应该在实例化时绑定到子事件.

                  Parents should bind to child events at the time of instantiation.

                  父母应该意识到"孩子,并且可以在必要时调用子方法 - 我通常会使用此方法而不是触发子事件,因为它更明确.我试图让孩子们独立于他们的父母,通过事件向上沟通.所以是的,深度嵌套的模型将通过事件链进行通信.

                  Parents should be "aware" of children, and can call child methods if necessary - I'd usually use this method over triggering a child event, as it's more explicit. I try to keep children independent of their parents, communicating upwards through events. So yes, deeply nested models would communicate through event chains.

                  我有时将 .set() 子属性作为 Backbone 属性,但通常只使用普通的 Javascript 属性(例如 this.child).这取决于上下文.使用 Backbone 属性提供 change 事件,因此如果您需要监视子项是否更改,请使用它们.骨干属性还使您能够在实例化时进行设置,例如var myModel = new Model({ other: otherModel }) - 但是因为父母通常会实例化他们的孩子,所以这并不真正适用于这种情况,我通常将该模式用于非分层模型 -模型关系.在大多数情况下,我将孩子设置为纯 Javascript 属性.

                  I sometimes .set() children as Backbone attributes, but usually just use plain Javascript attributes (e.g. this.child). It depends on the context. Using Backbone attributes provides change events, so if you need to monitor whether a child changes, use those. Backbone attributes also give you the ability to set at instantiation time, e.g. var myModel = new Model({ other: otherModel }) - but because parents usually instantiate their children, this doesn't really apply in that case, and I usually use that pattern for non-hierarchical model-model relationships. In most cases, I set children as plain Javascript attributes.

                  这篇关于让模型听嵌套模型和集合的最佳模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                    <bdo id='Li9M0'></bdo><ul id='Li9M0'></ul>

                      <small id='Li9M0'></small><noframes id='Li9M0'>

                        <tfoot id='Li9M0'></tfoot>
                        <i id='Li9M0'><tr id='Li9M0'><dt id='Li9M0'><q id='Li9M0'><span id='Li9M0'><b id='Li9M0'><form id='Li9M0'><ins id='Li9M0'></ins><ul id='Li9M0'></ul><sub id='Li9M0'></sub></form><legend id='Li9M0'></legend><bdo id='Li9M0'><pre id='Li9M0'><center id='Li9M0'></center></pre></bdo></b><th id='Li9M0'></th></span></q></dt></tr></i><div id='Li9M0'><tfoot id='Li9M0'></tfoot><dl id='Li9M0'><fieldset id='Li9M0'></fieldset></dl></div>
                        <legend id='Li9M0'><style id='Li9M0'><dir id='Li9M0'><q id='Li9M0'></q></dir></style></legend>

                              <tbody id='Li9M0'></tbody>