发布于 2015-08-14 15:14:31 | 552 次阅读 | 评论: 0 | 来源: 网络整理

保存应用程序的URL,需要使用导航方法来更新URL。

语法


router.navigate(fragment, options)

参数:

  • fragment: url将此参数之后显示的参数的名称。
  • options: 触发位置,选项替换调用路由功能和更新的URL。

示例


<!DOCTYPE html>
   <head>
      <title>Router Example</title>
         <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
   </head>
      <script type="text/javascript">
         //'RouteMenu' is a name of the view class
         var RouteMenu = Backbone.View.extend({
            el: '#routemenu',   //'el' defines which element to be used as the view reference

             //defines a click event to be occur on link
            events: {
               'click a' : 'onClick'
            },

            //After clicking on a link, router calls 'navigate' to update URL
            onClick: function( e ) {

               //Uses the navigate() method save the application as URL
               router.navigate('/');
            }
        });
        var Router = Backbone.Router.extend({

           //The 'routes' maps URLs with parameters to functions on your router
           routes: {
              'route/:id' : 'defaultRoute'
           },
        });

        //'routemenu' is an instance of the view class
        var routemenu = new RouteMenu();

         //It start listening to the routes and manages the history for bookmarkable URL's
        Backbone.history.start();
     </script>
  <body>
     //It refers to the view class 'RouteMenu' and creates the 3 links which changes the url when you click on the links
     <section id="routemenu">
        <ul>
           <li> <a href="#/route/1">route 1 </a> </li>
           <li> <a href="#/route/2">route 2 </a> </li>
           <li> <a href="#/route/3">route 3 </a> </li>
       </ul>
    </section>
  </body>
</html>

输出

让我们进行以下步骤来看看上面的代码工作:

  • 保存上述代码到文件navigate.html

  • 在浏览器打开这个HTML文件。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务