简体   繁体   English

骨干视图和路由器-视图多次实例化

[英]backbone view and router - view instanciated multiple times

I have the following router var watchlistEditView = new WatchlistEditView({ }); 我有以下路由器var watchlistEditView = new WatchlistEditView({});

router.on('route:editWatchlist', function(id) {
    console.log("routing to editWachlist");
    console.log(id);

    var watchlistEditView = new WatchlistEditView({ });
    watchlistEditView.render({id: id});
});

I do notice that I have multiple instances of the view so when I perform an operation, it does trigger on every instances, which is not what I want. 我确实注意到我有多个视图实例,所以当我执行一个操作时,它确实会在每个实例上触发,这不是我想要的。

Is it ok to create the new view each time the router get called? 每次调用路由器时都可以创建新视图吗? How could I prevent the view to get instanciated multiple times? 如何防止视图多次实例化?

Thanks 谢谢

This is something to make a good research as it involves many things. 这涉及很多事情,因此需要进行很好的研究。 Memory management, listener bindings and things that actually backbone is handling every moment you create and destroy a view. 内存管理,侦听器绑定以及实际上是骨干的事情在您创建和销毁视图时都会处理。 I recommend reading this article. 我建议阅读这篇文章。

https://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/ https://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

Actually I create and destroy what I need using backbone marionette framework and behind the scenes when working on layouts and backbone views it makes several checks as if the view is rendered if I am adding a new view to a region with a view already, and other things. 实际上,我使用骨架木偶框架创建和销毁我需要的东西,并且在布局和骨架视图上工作时在幕后进行了很多检查,就像是否要向已添加视图的区域添加新视图一样渲染视图。东西。

I recommend keep on creating them, and looking when to destroy them when it is necessary. 我建议继续创建它们,并在必要时寻找何时销毁它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM