主要成分:刷新两次相同的路由路径 [英] Backbone: Refresh the same route path for twice

查看:126
本文介绍了主要成分:刷新两次相同的路由路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,骨干路由器的导航方法将不会重新加载的路径,如果是已经在该路由路径。

I noticed that backbone's router navigate method won't reload the path, if it is already at that route path.

例如在路径/厂景已路线,你还记得router.navigate('厂景',{触发:真});不会再次激活路径事件。

For example the route already at path /view1, you recall router.navigate('view1',{trigger:true}); won't activate the route event again.

Here'the code我测试:

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

在code以上将加载厂景路径,打印出厂景。但是如果你试图点击该按钮,导航到厂景的道路,它会被忽略。

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

我的问题是我怎么激活路径事件,如果它已经在相同的路径。

My question is how do I reactivate the route event, if it already at the same 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. 您可以只直接调用该方法的路线: sys.view1();

您可以在片段设置为另一个死的路线,然后再返回到你想要的路线。

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


sys.navigate('someDeadRoute');
sys.navigate('厂景',{触发:真});

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

You could stop and restart the router:

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

这将再次皮卡路线并运行它。

This would pickup the route again and run it.

我想我会用#1去了。

这篇关于主要成分:刷新两次相同的路由路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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