BackboneJS - 歷史



它保留了一個歷史記錄,匹配適當的路由,啟動回撥來處理事件以及啟用應用程式中的路由。

start

這是唯一可以用來操作 BackboneJS-History 的方法。它開始傾聽路由並管理可書籤 URL 的歷史記錄。

語法

Backbone.history.start(options)

引數

options - 選項包括與歷史記錄一起使用的引數,如 pushStatehashChange

例項

<!DOCTYPE html>
<html>
   <head>
      <title>History 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">
      //'Router' is a name of the router class
      var Router = Backbone.Router.extend ({

         //The 'routes' maps URLs with parameters to functions on your router
         routes: {
            "myroute" : "myFunc"
         },

         //'The function 'myFunc' defines the links for the route on the browser
         myFunc: function (myroute) {
            document.write(myroute);
         }
      });

      //'router' is an instance of the Router
      var router = new Router();

      //Start listening to the routes and manages the history for bookmarkable URL's
      Backbone.history.start();
   </script>
   
   <body>
      
      <a href = "#route1">Route1 </a>
      <a href = "#route2">Route2 </a>
      <a href = "#route3">Route3 </a>
   </body>
   
</html>

輸出

讓我們執行以下步驟,檢視上面的程式碼是如何工作的 -

  • 將以上程式碼儲存在 start.htm 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案。

注意 - 以上功能與位址列有關。因此,如果你在瀏覽器中開啟上面的程式碼,它將顯示如下結果。

start example

點選此處檢視演示

廣告
© . All rights reserved.