Pattern to manage views in backbone

后端 未结 4 1684
误落风尘
误落风尘 2021-01-30 09:34

Coming from GWT, Backbone seems to miss a built-in solution on how to handle the life-cycle of a view. In GWT, every activity, which is more or less the equivalent to a View in

4条回答
  •  自闭症患者
    2021-01-30 09:58

    I post my solution to manage view at https://github.com/thomasdao/Backbone-View-Manager.

    The view manager will always cleanup existing view before create a new view. Now I will initialize a new view by:

    newView = VM.createView("newView", function(){
                 return new View();
              };
    

    If I want to reuse a view instead of creating a new one, I can use

    newView = VM.reuseView("newView", function() {
                  return new View();
              }
    

    The different between VM.reuseView and VM.createView is that, reuseView will look for existing view with name "newView", if found it will return to you. Else, it will execute the callback function and cache result. VM.createView will always execute the callback function and cleanup existing view for you. Hence you may like to use VM.createView if the views is dynamic and frequently change

提交回复
热议问题