骨干网:刷新当前路由 [英] Backbone: Refresh the current route

查看:92
本文介绍了骨干网:刷新当前路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到骨干路由器的导航方法不会重新加载当前路径.

I noticed that the backbone router's navigate method won't reload the current path.

例如,当前路线为/view1,而您呼叫router.navigate('view1',{trigger:true});.它不会再次激活路由事件.

For example the current route is /view1 and you call router.navigate('view1',{trigger:true});. It won't activate the route event again.

这是我测试的代码:

<html>
<head>
    <title>Testing123</title>
    <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
    <script src="http://underscorejs.org/underscore-min.js" type="text/javascript"></script>
    <script src="http://backbonejs.org/backbone-min.js" type="text/javascript"></script>
    <style type="text/css" media="screen">
        #box{
            width:400px;
            height:400px;
            background-color:red;
        }       
    </style>
</head>
<body>
    <button id='view1'>view1</button>
    <button id='view2'>view2</button>
    <br/>
    <div id='box'>

    </div>
    <script type="text/javascript" charset="utf-8" async defer>
        var SystemRouter = Backbone.Router.extend({

          routes: {
            "view1":"view1",
            "view2":"view2"
          },

          view1: function() {
            console.log('view1');
          },

          view2: function() {
            console.log('view2');
          }

        });

        var sys = new SystemRouter();
        Backbone.history.start({pushState: true, root: "/test/routing.html#/"});
        sys.navigate('view1');

        $('#view1').click(function(){
            sys.navigate('view1',{trigger:true});
        });

        $('#view2').click(function(){
           sys.navigate('view2',{trigger:true});
        }); 
    </script>
</body>
</html>

上面的代码将加载/view1路径,该路径会打印出"view1".但是,如果您尝试单击该按钮导航到/view1路径,它将被忽略.

The code above will load /view1 path, which prints out 'view1'. But if you try to click the button to navigate to /view1 path, it will be ignored.

我的问题是:如果路由事件与当前路径相同,该如何调用该路由事件?

My question is: How do I call the route event if it is the same as the current path?

推荐答案

有几件事可以完成此任务.如果您希望导航到URL中已经存在的路由,请首先使用Backbone.history.fragment检查当前路由片段,然后查看它是否与您要激活的路由相同.如果是这样,那么您可以执行以下任一操作:

There are several things that would accomplish this. If you anticipate navigating to a route that is already in the url, then first check the current route fragment with Backbone.history.fragment and see if it is the same as the route you want to activate. If so, then you can do any of the following:

  1. 您可以直接调用route方法:sys.view1();

您可以将片段设置为另一条无效路径,然后返回所需的路径.

You could set the fragment to another dead route, then back to the route you want.

sys.navigate('someDeadRoute'); sys.navigate('view1',{trigger: true});

sys.navigate('someDeadRoute'); sys.navigate('view1',{trigger: true});

您可以停止并重新启动路由器:

You could stop and restart the router:

Backbone.history.stop(); Backbone.history.start()

Backbone.history.stop(); Backbone.history.start()

这将再次拾取路线并运行.

This would pickup the route again and run it.

我想我会选择#1.

这篇关于骨干网:刷新当前路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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