Backbone.js parent 对比 child 观看次数

Backbone.js parent vs. child views

使用 Backbone.js 进行 front-end 开发,是否有可靠的方法在渲染其各自的 parent 视图之前渲染 child 视图?有这方面的模式吗?我能够呈现 child 视图,但我必须小心始终首先呈现 parent 视图。是否有在 parents 之前安全渲染 children 的模式?

有一种方法可以做到这一点,使用 jQuery。 jQuery 有一个 find() 函数。 https://api.jquery.com/find/

假设我有一个 Backbone.View 实例,它创建并写入其自定义元素 (el),默认情况下它是一个 div 元素。下面的代码在我视图的渲染函数中:

              var self = this;

              var ret = EJS.render($template, {});  //get the template html

              self.$el.html(ret);  //put the html string into self.$el (still not in the DOM though!!)

              //Facebook's React.js lib
              React.render(
                  <TimerExample start={Date.now()} />,
                   $(self.el).find('#react-timer-example-div-id')[0]
               );


              React.render(
                  <MenuExample items={ ['Home', 'Services', 'About', 'Contact us'] } />,
                  $(self.el).find('#react-menu-example-div-id')[0]
               );


             //using the two-way data binding lib "Rivets"
             Rivets.bind( $(self.el).find('#some-id')[0], {obj: exampleObj})

上面的方法有效,我认为这是将子视图渲染到父视图的一个很好的解决方案,而无需先将父视图放在 DOM 中。正如我所说,诀窍是使用 $(this.el).find('#xyz');。如果有更好的方法,我很想听听,但这似乎很有效。

我的上述模板由 $template 表示,如下所示:

<div>
    <div id="react-timer-example-div-id">timer example goes here</div>
    <div id="react-menu-example-div-id">menu example goes here</div>
    <div id="some-id">blah blah</div>
</div>