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. 單獨使用

<!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: 當前注冊路徑僅執行一次的方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM