鍍金池/ 教程/ HTML/ BackboneJS router.navigate()方法
BackboneJS Collection.extend()方法
BackboneJS collection.pop()方法
BackboneJS collection.set()方法
BackboneJS collection.remove()方法
BackboneJS view.attributes
Backbone.View.extend()方法
BackboneJS router.route()方法
BackboneJS model.destroy()方法
BackboneJS視圖初始化
BackboneJS事件stopListening
BackboneJS collection.clone()方法
BackboneJS router.navigate()方法
BackboneJS model.idAttribute屬性
BackboneJS router.execute()方法
BackboneJS模型
BackboneJS model.previousAttributes()方法
BackboneJS model.escape()方法
BackboneJS model.previous()方法
BackboneJS應(yīng)用
BackboneJS view.setElement()方法
BackboneJS collection.findWhere()方法
BackboneJS collection.slice()方法
BackboneJS view.el
BackboneJS集合
BackboneJS model.unset()方法
BackboneJS collection.comparator屬性
BackboneJS collection.push()方法
BackboneJS model.save()方法
BackboneJS collection.shift()方法
BackboneJS collection.parse()方法
BackboneJS Collection.model
BackboneJS教程
BackboneJS model.defaults
BackboneJS事件trigger
BackboneJS collection.sync()方法
BackboneJS model.has()方法
BackboneJS model.isNew()方法
BackboneJS model.url()方法
BackboneJS Model.get()方法
BackboneJS視圖
BackboneJS model.clone()方法
BackboneJS collection.toJSON()方法
BackboneJS Backbone.emulateHTTP
BackboneJS Router初始化
BackboneJS環(huán)境設(shè)置
BackboneJS model.fetch()方法
BackboneJS事件
BackboneJS Model.set()方法
BackboneJS router.routes
BackboneJS collection.unshift()方法
BackboneJS collection.add()方法
BackboneJS collection.models
BackboneJS model.hasChanged()方法
BackboneJS collection.url()方法
BackboneJS Backbone.history.start()方法
BackboneJS model.validate()方法
BackboneJS collection.create()方法
BackboneJS model.parse()方法
BackboneJS事件on
BackboneJS collection.at()方法
BackboneJS事件off
BackboneJS view.$(selector)方法
BackboneJS .sync()方法
BackboneJS collection.sort()函數(shù)
BackboneJS collection.length
BackboneJS model.changedAttributes()方法
BackboneJS model.isValid()方法
BackboneJS model.attributes屬性
BackboneJS collection.reset()方法
BackboneJS model.validationError
BackboneJS model.clear()方法
BackboneJS collection.get(id)方法
BackboneJS model.changed
BackboneJS同步
BackboneJS 集合初始化
BackboneJS collection.fetch()方法
BackboneJS model.sync()方法
BackboneJS Model.extend()方法
BackboneJS model.id屬性
BackboneJS model.urlRoot()方法
BackboneJS路由
BackboneJS事件once
BackboneJS collection.pluck()方法
BackboneJS view.template(data)方法
BackboneJS事件listenTo
BackboneJS collection.where()方法
BackboneJS model.toJSON()方法
BackboneJS Backbone.emulateJSON
BackboneJS Model.initialize()方法
BackboneJS view.$el
BackboneJS事件listenToOnce
BackboneJS model.cid屬性

BackboneJS router.navigate()方法

保存應(yīng)用程序的URL,需要使用導(dǎo)航方法來更新URL。

語(yǔ)法

router.navigate(fragment, options)

參數(shù):

  • fragment: url將此參數(shù)之后顯示的參數(shù)的名稱。
  • options: 觸發(fā)位置,選項(xiàng)替換調(diào)用路由功能和更新的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>

輸出

讓我們進(jìn)行以下步驟來看看上面的代碼工作:

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

  • 在瀏覽器打開這個(gè)HTML文件。