多个匹配的路由 [英] multiple matching routes

查看:105
本文介绍了多个匹配的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序Backbone.js的定义两个控制器,且控制器均定义匹配的location.hash哪条路线模式。我遇到了麻烦他们两个火 - 例如

I've got a backbone.js application that defines two controllers, and the controllers both define route patterns which match the location.hash. I'm having trouble getting both of them to fire - e.g.

ManagerController = Backbone.Controller.extend({
   routes: {
      ":name":      "doStuff"
   },

   doStuff : function(name) {
      console.log("doStuff called...");
   }
});

Component1Controller = Backbone.Controller.extend({
   routes: {
      "xyz123":      "doMoreStuff"
   },

   doMoreStuff : function() {
      console.log("doMoreStuff called...");
   }
});

因此​​,如果URL为http://mysite.com/#xyz123,然后我看到'doStuff()调用,或者如果我注释掉这条路线,那么doMoreStuff()被调用。但不能同时使用。

so if the url is "http://mysite.com/#xyz123", then I am seeing 'doStuff()' called, or if I comment out that route, then 'doMoreStuff()' is called. But not both.

我使用这个架构,因为我的网页是高度​​面向组件,每个组件定义了自己的控制器。 A组件管理器还定义了一个控制器,它确实所有路线的一些内部管理。

I'm using this architecture because my page is highly component oriented, and each component defines its own Controller. A 'component manager' also defines a Controller which does some house keeping on all routes.

我应该能够配置两个控制器,既要原路回应?欢呼声中,

Should I be able to configure two controllers that both respond to the same route? Cheers,

科林

推荐答案

简短的回答:不,你不能这样做。每页一个控制器。

Short answer: No, you can't do that. One Controller per page.

龙回答:当你实例化一个新的控制器,它增加了其路由到历史单。历史单被监测URL的哈希组成部分,而当散列改变,它扫描路线为的第一个的前pression符合其需求。然后触发与该路由(即功能已被结合到在其被声明的控制器)相关联的功能。只会触发一次,并且如果存在冲突,其中它触发的顺序是正式不确定的。 (实际上它可能是确定的。)

Long answer: When you instantiate a new Controller, it adds its routes to the History singleton. The History singleton is monitoring the hash component of the URL, and when the hash changes, it scans the routes for the first expression that matches its needs. It then fires the function associated with that route (that function has been bound to the controller in which it was declared). It will only fire once, and if there is a conflict the order in which it fires is formally indeterminate. (In practice it's probably deterministic.)

哲学答案:控制器是一个说法对象,它影响基于URL的哈希部分对整个页面的presentation。其目的是为了提供书签功能的网址,用户可以在将来达到,这样当他去的URL,他可以从中间许多$ P $对所选视图启动。从你的描述,这听起来像你这种操作暴露公开,手动寻址的项目来操作你的视口的不同部分,而把其他人孤单。这不是它是如何工作。

Philosophical answer: The controller is a "view" object which affects the presentation of the whole page based on the hash component of the URL. Its purpose is to provide bookmark-capable URLs that the user can reach in the future, so that when he goes to a URL he can start from a pre-selected view among many. From your description, it sounds like you're manipulating this publicly exposed, manually addressable item to manipulate different parts of your viewport, while leaving others alone. That's not how it works.

一个关于骨干的好处之一是,如果你传递这已经是一个常规的前pression的路线,它会使用它原样。所以,如果你想使用控制器(在显示模式B左上角的组件1在显示模式A的右上角,组件2等),以创建布局的可收藏描述我可以建议一些alternatives--的分配每一个命名空间中的URL的哈希部分,并创建忽略其他路线,即

One of the nice things about Backbone is that if you pass it a route that's already a regular expression, it will use it as-is. So if you're trying to use the controller to create a bookmarkable description of the layout (component 1 in the upper right hand corner in display mode "A", component 2 in the upper left corner in display mode "B", etc) I can suggest a number of alternatives-- allocate each one a namespace in the hash part of the URL, and create routes that ignore the rest, i.e.

routes: {
    new RegExp('^([^\/]*)/.*$'): 'doComponent1stuff',
    new RegExp('^[^\/]*/([^\/]*)\/.*$': 'doComponent2stuff',
}

请参阅第一个斜杠后的第一个如何使用的唯一项目,第二个,第二斜线后等,您可以连接code你的魔法完全是你想怎么。

See how the first uses only items after the first slash, the second after the second slash, etc. You can encode your magic entirely how you want.

我建议,不过,如果你打算做与组件的外观和感觉的东西,你想,要合理持久的,你看看,并得到一些地方设置了Cookie的意见商店;如果他们足够小,饼干就足够了。

I suggest, though, that if you're going to be doing something with the look and feel of the components, and you want that to be reasonably persistent, that you look into the views getting and setting their cookies from some local store; if they're small enough, cookies will be enough.

这篇关于多个匹配的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆