简体   繁体   English

使用骨干路由器导航

[英]Navigating with the Backbone router

I have a Backbone.Router class setup and everything is working perfectly. 我有一个Backbone.Router类设置,并且一切正常。 However, there is a little (maybe a bit blocking) inconvenience. 但是,有一些不便之处(可能有些阻碍)。

In order to navigate using the Backbone Router, I use the following code 为了使用骨干路由器进行导航,我使用以下代码

myrouter.navigate('/search_term/2/filter1+filter2/');

This works as it should. 这可以正常工作。 However, the different parameters of the URL are not updated in the same place. 但是,URL的不同参数不会在同一位置更新。 For example, the user might move to the next page (3) and the search term and filters shouldn't be updated. 例如,用户可能会移至下一页(3),并且不应更新搜索词和过滤器。

What I want to be able to do is something like this 我想要做的就是这样

myrouter.navigate(':route_id', ':route_value');

For example, I can do 例如,我可以

myrouter.navigate(':query', 'search_term');
myrouter.navigate(':pagination', '5');

Can Backbone do this? 骨干可以做到这一点吗? If not, any possible solutions? 如果没有,有什么可能的解决方案?

you can implement a function your self that extends the backbone routes to work with the way you want. 您可以自己实现一个功能,该功能可以扩展主干路由,以所需的方式工作。

If you read routes source code, you will see that they only convert you pattern for a regexp pattern, is very simple, then apply the method target with the arguments matched by the regexp. 如果您阅读路由源代码,您将看到它们仅将您的模式转换为regexp模式,这非常简单,然后将方法目标与regexp匹配的参数一起应用。

So if you want a reverse route to navigate or something like that you can apply your self extending Backbone.Route 因此,如果您想要反向路线导航或类似的东西,则可以应用自扩展Backbone.Route

Something like this: 像这样:

myrouter = Backbone.Router.extends({
    navigate: function(a, b) {
        // Do something with my args here
        var result = doSomething(a, b);

        // Then execute backbone's official navigate with the results
        Backbone.Router.navigate.apply(this, result);
    }
});

Create your function to doSomething to work's how you need to reverse create your route. 创建函数doSomething工作就是你需要扭转创建您的路线。

But there isn't a Backbone native way to do this, the router ins't mutch advanced feature. 但是,没有Backbone本机方式可以做到这一点,路由器并没有先进的功能。

I ended up fixing this myself. 我最终自己解决了这个问题。

The solution was to listen to the change of the models by binding to the change event in a similar fashion to what I do when submitting a request. 解决方案是通过绑定更改事件来监听模型的更改,其方式与提交请求时的方式类似。

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

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