How to handle initializing and rendering subviews in Backbone.js?

前端 未结 7 1173
北海茫月
北海茫月 2020-11-29 14:38

I have three different ways to initialize and render a view and its subviews, and each one of them has different problems. I\'m curious to know if there is a better way that

7条回答
  •  一整个雨季
    2020-11-29 14:43

    What I do is giving each children an identity (which Backbone has already done that for you: cid)

    When Container does the rendering, using the 'cid' and 'tagName' generate a placeholder for every child, so in template the children has no idea about where it will be put by the Container.

    
    

    than you can using

    Container.render()
    Child.render();
    this.$('#'+cid).replaceWith(child.$el);
    // the rapalceWith in jquery will detach the element 
    // from the dom first, so we need re-delegateEvents here
    child.delegateEvents();
    

    no specified placeholder is needed, and Container only generate the placeholder rather than the children's DOM structure. Cotainer and Children are still generating own DOM elements and only once.

提交回复
热议问题