码迷,mamicode.com
首页 > Web开发 > 详细

director.js实现前端路由

时间:2016-10-10 09:36:38      阅读:662      评论:0      收藏:0      [点我收藏+]

标签:

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

director.js是什么?

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

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

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

简单例子

1. 单独使用

技术分享
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>A Gentle Introduction</title>
    <script
      src="https://rawgit.com/flatiron/director/master/build/director.min.js">
    </script>
    <script>
      var author = function () { console.log("author"); };
      var books = function () { console.log("books"); };
      var viewBook = function (bookId) {
        console.log("viewBook: bookId is populated: " + bookId);
      };
      var routes = {
        /author: author,
        /books: [books, function() {
          console.log("An inline route handler.");
        }],
        /books/view/:bookId: viewBook
      };
      var router = Router(routes);
      router.init();
    </script>
  </head>
  <body>
    <ul>
      <li><a href="#/author">#/author</a></li>
      <li><a href="#/books">#/books</a></li>
      <li><a href="#/books/view/1">#/books/view/1</a></li>
    </ul>
  </body>
</html>
View Code

2当与jquery相结合

技术分享
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>A Gentle Introduction 2</title>
    <script
      src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js">
    </script>
    <script
      src="https://rawgit.com/flatiron/director/master/build/director.min.js">
    </script>
    <script>
    $(document).ready(function() {
      //
      // create some functions to be executed when
      // the correct route is issued by the user.
      //
      var showAuthorInfo = function () { console.log("showAuthorInfo"); };
      var listBooks = function () { console.log("listBooks"); };
      var allroutes = function() {
        var route = window.location.hash.slice(2);
        var sections = $(section);
        var section;
        section = sections.filter([data-route= + route + ]);
        if (section.length) {
          sections.hide(250);
          section.show(250);
        }
      };
      //
      // define the routing table.
      //
      var routes = {
        /author: showAuthorInfo,
        /books: listBooks
      };
      //
      // instantiate the router.
      //
      var router = Router(routes);
      //
      // a global configuration setting.
      //
      router.configure({
        on: allroutes
      });
      router.init();
    });
    </script>
  </head>
  <body>
    <section data-route="author">Author Name</section>
    <section data-route="books">Book1, Book2, Book3</section>
    <ul>
      <li><a href="#/author">#/author</a></li>
      <li><a href="#/books">#/books</a></li>
    </ul>
  </body>
</html>
View Code

Director支持commond的书写方式

例子如下:

技术分享
 var director = require(‘director‘);
  var router = new director.cli.Router();
  router.on(‘create‘, function () {
    console.log(‘create something‘);
  });
  router.on(/destroy/, function () {
    console.log(‘destroy something‘);
  });
  // You will need to dispatch the cli arguments yourself
  router.dispatch(‘on‘, process.argv.slice(2).join(‘ ‘));
View Code

初始化及路由器的注册

技术分享
 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: 当前注册路径仅执行一次的方法

director.js实现前端路由

标签:

原文地址:http://www.cnblogs.com/mj878/p/5944552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!