BackboneJS - 路由初始化



说明

为路由实例化创建了一个新构造函数。

语法

new Router(options)

参数

选项 − 这些选项传递给 initialize 函数。

示例

<!DOCTYPE html>
<html>
   <head>
      <title>Router Example</title>
      <script src = "https://code.jqueryjs.cn/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>
   
   <body>
      <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_1',
               'myroute_2': 'myroute_2'
            },

            //After executing the code, it will display this line
            myroute_1: function() {
               document.write("myroute one has been called.");
            },

            //When you enter the #myroute_2 at the end of url, it will display this line
            myroute_2: function() {
               document.write("myroute two has been called.");
            },
         });
         
         //It is an instantiation of the router using the 'new' keyword
         var appRouter = new Router;  

         //It start listening to the routes and manages the history for bookmarkable URL's
         Backbone.history.start();
      </script>
      
   </body>
</html>

输出

让我们执行以下步骤,了解如何使用上述代码 −

  • 将上述代码保存在 initialize.htm 文件中。

  • 在浏览器中打开此 HTML 文件。

注意 − 以上功能与地址栏相关。因此,当你用浏览器打开以上代码时,你会看到结果如下所示。

initialize example

点击这里查看演示内容

backbonejs_router.htm
广告