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

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

问题描述

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

例如当前路由是 /view1 并且你调用 router.navigate('view1',{trigger:true});.它不会再次激活路由事件.

这是我测试的代码:

<头><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">#盒子{宽度:400px;高度:400px;背景颜色:红色;}</风格><身体><button id='view1'>view1</button><button id='view2'>view2</button><br/><div id='box'>

<script type="text/javascript" charset="utf-8" async defer>var SystemRouter = Backbone.Router.extend({路线:{"view1":"view1",视图2":视图2"},视图1:函数(){console.log('view1');},视图2:函数(){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});});

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

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

解决方案

有几种方法可以实现这一点.如果您希望导航到 url 中已有的路由,则首先使用 Backbone.history.fragment 检查当前路由片段,看看它是否与您要激活的路由相同.如果是这样,那么您可以执行以下任一操作:

  1. 你可以直接调用路由方法:sys.view1();

  2. 您可以将片段设置为另一条死路,然后返回到您想要的路线.

    <代码>sys.navigate('someDeadRoute');sys.navigate('view1',{trigger: true});

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

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

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

我想我会选择#1.

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

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

Here's the code I test:

<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>

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?

解决方案

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. You could just call the route method directly: sys.view1();

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

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

  3. You could stop and restart the router:

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

    This would pickup the route again and run it.

I think I would go with #1.

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

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