简体   繁体   English

Backbone.js:嵌套视图的路由

[英]Backbone.js: Routing for nested views

I'm trying to figure out following scenario: 我想弄清楚以下情况:

Lets say that I have two views: one for viewing items and one for buying them. 让我们说我有两个观点:一个用于查看项目,另一个用于购买它们。 The catch is that buying view is a sub view for viewing. 问题在于购买视图是用于查看的子视图。

For routing I have: 对于路由,我有:

var MyRouter = Backbone.Router.extend({
  routes: {
    'item/:id': 'viewRoute',
    'item/:id/buy': 'buyRoute'
  }
});

var router = new MyRouter;

router.on("route:viewRoute", function() {
  // initialize main view
  App.mainview = new ViewItemView();

});

router.on("route:buyRoute", function() {
  // initialize sub view
  App.subview = new BuyItemView();
});

Now if user refreshes the page and buyRoute gets triggered but now there is no main view. 现在,如果用户刷新页面并且buyRoute被触发,但现在没有主视图。 What would be best solution to handle this? 处理这个问题的最佳解决方案是什么?

I am supposed that the problem you are having right now is that you don't want to show some of the stuff inside ViewItem inside BuyView? 我认为你现在遇到的问题是你不想在BuyView中的ViewItem中显示一些内容吗? If so then you should modularized what BuyView and ViewItem have in common into another View then initialize it on both of those routes. 如果是这样,那么你应该将BuyView和ViewItem的共同点模块化到另一个View中,然后在这两个路径上初始化它。

Here is a code example from one of my apps 这是我的一个应用程序的代码示例

https://github.com/QuynhNguyen/Team-Collaboration/blob/master/app/scripts/routes/app-router.coffee https://github.com/QuynhNguyen/Team-Collaboration/blob/master/app/scripts/routes/app-router.coffee

As you can see, I modularized out the sidebar since it can be shared among many views. 正如您所看到的,我模块化了侧边栏,因为它可以在许多视图之间共享。 I did that so that it can be reused and won't cause any conflicts. 我这样做是为了可以重复使用,不会引起任何冲突。

You could just check for the existence of the main view and create/open it if it doesn't already exist. 您可以检查是否存在主视图,如果它尚不存在则创建/打开它。

I usually create (but don't open) the major views of my app on booting up the app, and then some kind of view manager for opening/closing. 我通常在启动应用程序时创建(但不要打开)我的应用程序的主要视图,然后创建一个用于打开/关闭的视图管理器。 For small projects, I just attach my views to a views property of my app object, so that they are all in one place, accessible as views.mainView, views.anotherView, etc. 对于小项目,我只是将我的视图附加到我的app对象的views属性,以便它们都在一个位置,可以作为views.mainView,views.anotherView等访问。

I also extend Backbone.View with two methods: open and close that not only appends/removes a view to/from the DOM but also sets an isOpen flag on the view. 我还使用两种方法扩展Backbone.View: openclose ,不仅可以向DOM添加/删除视图,还可以在视图上设置isOpen标志。

With this, you can check to see if a needed view is already open, then open it if not, like so: 有了这个,您可以检查是否已经打开了所需的视图,如果没有,则打开它,如下所示:

if (!app.views.mainView.isOpen) {
    // 
}

An optional addition would be to create a method on your app called clearViews that clears any open views, perhaps with the exception of names of views passed in as a parameter to clearViews . 一个可选的补充是在你的应用程序上创建一个名为clearViews的方法,它清除所有打开的视图,可能除了作为参数传递给clearViews的视图名称clearViews So if you have a navbar view that you don't want to clear out on some routes, you can just call app.clearViews('topNav') and all views except views.topNav will get closed. 因此,如果您有一个导航栏视图,您不想在某些路线上清除,您可以调用app.clearViews('topNav') ,除了views.topNav之外的所有视图都将关闭。

check out this gist for the code for all of this: https://gist.github.com/4597606 查看这个要点的所有要点: https//gist.github.com/4597606

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

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