director.js实现前端路由

注:director.js的官网 https://github.com/flatiron/director

director.js是什么?

理解:前端的route框架,director.js客户端的路由注册/解析器,在不刷新的情况下,利用“#”号组织不同的URL路径,并根据不同的URL路径进行不同的方法调用。意思就是有什么样的路径就有什么样的方法。

场合:客户端浏览器和node.js的服务器应用。非常适合用来开发不需要刷新的单页面应用程序以及node.js应用。

兼容性:不依赖与任何库。例如jquery等。但它又和jquery能很好的融合在一起; 
客户端的路由: 
客户端的路由 (也称为哈希路由) 允许您指定一些关于使用URL应用状态的信息,当用户指定固定的URL,进行相应的页面显示。

简单例子

1. 单独使用

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="utf-8">
 5     <title>A Gentle Introduction</title>
 6     <script
 7       src="https://rawgit.com/flatiron/director/master/build/director.min.js">
 8     </script>
 9     <script>
10       var author = function () { console.log("author"); };
11       var books = function () { console.log("books"); };
12       var viewBook = function (bookId) {
13         console.log("viewBook: bookId is populated: " + bookId);
14       };
15       var routes = {
16         '/author': author,
17         '/books': [books, function() {
18           console.log("An inline route handler.");
19         }],
20         '/books/view/:bookId': viewBook
21       };
22       var router = Router(routes);
23       router.init();
24     </script>
25   </head>
26   <body>
27     <ul>
28       <li><a href="#/author">#/author</a></li>
29       <li><a href="#/books">#/books</a></li>
30       <li><a href="#/books/view/1">#/books/view/1</a></li>
31     </ul>
32   </body>
33 </html>
View Code

 

2当与jquery相结合

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="utf-8">
 5     <title>A Gentle Introduction 2</title>
 6     <script
 7       src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js">
 8     </script>
 9     <script
10       src="https://rawgit.com/flatiron/director/master/build/director.min.js">
11     </script>
12     <script>
13     $('document').ready(function() {
14       //
15       // create some functions to be executed when
16       // the correct route is issued by the user.
17       //
18       var showAuthorInfo = function () { console.log("showAuthorInfo"); };
19       var listBooks = function () { console.log("listBooks"); };
20       var allroutes = function() {
21         var route = window.location.hash.slice(2);
22         var sections = $('section');
23         var section;
24         section = sections.filter('[data-route=' + route + ']');
25         if (section.length) {
26           sections.hide(250);
27           section.show(250);
28         }
29       };
30       //
31       // define the routing table.
32       //
33       var routes = {
34         '/author': showAuthorInfo,
35         '/books': listBooks
36       };
37       //
38       // instantiate the router.
39       //
40       var router = Router(routes);
41       //
42       // a global configuration setting.
43       //
44       router.configure({
45         on: allroutes
46       });
47       router.init();
48     });
49     </script>
50   </head>
51   <body>
52     <section data-route="author">Author Name</section>
53     <section data-route="books">Book1, Book2, Book3</section>
54     <ul>
55       <li><a href="#/author">#/author</a></li>
56       <li><a href="#/books">#/books</a></li>
57     </ul>
58   </body>
59 </html>
View Code

 

Director支持commond的书写方式

例子如下:

 1 var director = require('director');
 2   var router = new director.cli.Router();
 3   router.on('create', function () {
 4     console.log('create something');
 5   });
 6   router.on(/destroy/, function () {
 7     console.log('destroy something');
 8   });
 9   // You will need to dispatch the cli arguments yourself
10   router.dispatch('on', process.argv.slice(2).join(' '));
View Code

 

初始化及路由器的注册

1 var router = Router(routes);
View Code

 

  另外,构造方法中传入的routes参数是一个路由对象,它是一个具有键值对结构的对象,可以被多层的嵌套。键对对应的URL中传入的路径,一般一个键值对应按照分割符切割后的某一部分;而键值对的值对应的该路径的需要触发的回调函数名。回调函数要在路由表对象使用前先声明,否则js会报错。 
  另外,回调函数除非特殊情况,一般不推荐使用匿名函数,请尽量先声明后使用。

  var routes = {
    '/dog': bark,    
    '/cat': [meow, scratch]
  };

这里的的url是#dog和#cat 
声明Router对象后,需要调用init()方法进行初始化,如:

router.init();

路由的事件

路由事件是路由注册表中一个有固定命名的属性,是指当路由方法router.dispatch()被调用时,路由匹配成功的时定义的需要触发的回调方法(允许定义多个回调方法)。上文即时注册功能里的"on"方法就是一个事件。具体信息如下:  

on :当路由匹配成功后,需要执行的方法 
before:在触发“on”方法之前执行的方法 
  仅在客户端有效的方法:

after:当离开当前注册路径时,需要执行的方法 
once: 当前注册路径仅执行一次的方法

posted @ 2017-04-09 15:28  yuwenjing  阅读(1158)  评论(0编辑  收藏  举报