Backbone.js的:获取当前路径 [英] Backbone.js: get current route

查看:99
本文介绍了Backbone.js的:获取当前路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用骨干,是否有可能对我来说,得到当前路由的名称?我知道如何绑定到路由变化事件,但我希望能够确定在其他时间目前的路线,在变化的。

Using Backbone, is it possible for me to get the name of the current route? I know how to bind to route change events, but I'd like to be able to determine the current route at other times, in between changes.

推荐答案

如果您在您的应用程序实例化一个路由器,下面一行返回当前片段:

If you have instantiated a Router in your application, the following line returns the current fragment:

Backbone.history.getFragment();

Backbone.js的文档


[...]
历史作为全球路由器(每帧)来处理hashchange事件或pushState的,匹配合适的路由,并触发回调。你不应该曾经有创建这些自己的一个 - 你应该使用将为创建的参考Backbone.history您自动如果您使用路由器与路线。
[...]

" [...] History serves as a global router (per frame) to handle hashchange events or pushState, match the appropriate route, and trigger callbacks. You shouldn't ever have to create one of these yourself — you should use the reference to Backbone.history that will be created for you automatically if you make use of Routers with routes. [...]"

如果您需要绑定到该片段函数的名称,就可以让这样的事情你的路由器的域内:

If you need the name of the function bound to that fragment, you can make something like this inside the scope of your Router:

alert( this.routes[Backbone.history.getFragment()] );

或者这样从路由器之外的:

Or like this from outside your router:

alert( myRouter.routes[Backbone.history.getFragment()] );

这篇关于Backbone.js的:获取当前路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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