首頁 > web前端 > js教程 > 主體

用director.js實作前端路由使用實例

高洛峰
發布: 2017-02-03 13:59:25
原創
1375 人瀏覽過

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 = {
    &#39;/author&#39;: author,
    &#39;/books&#39;: [books, function() {
     console.log("An inline route handler.");
    }],
    &#39;/books/view/:bookId&#39;: 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>
登入後複製

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>
  $(&#39;document&#39;).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 = $(&#39;section&#39;);
    var section;
    section = sections.filter(&#39;[data-route=&#39; + route + &#39;]&#39;);
    if (section.length) {
     sections.hide(250);
     section.show(250);
    }
   };
   //
   // define the routing table.
   //
   var routes = {
    &#39;/author&#39;: showAuthorInfo,
    &#39;/books&#39;: 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>
登入後複製

Director支持commond的書寫方式

例子如下:

var director = require(&#39;director&#39;);
var router = new director.cli.Router();
router.on(&#39;create&#39;, function () {
 console.log(&#39;create something&#39;);
});
router.on(/destroy/, function () {
 console.log(&#39;destroy something&#39;);
});
// You will need to dispatch the cli arguments yourself
router.dispatch(&#39;on&#39;, process.argv.slice(2).join(&#39; &#39;));
登入後複製

初始化及路由器的註冊

var router = Router(routes);
登入後複製

另外,建構方法中傳入的routes參數是一個路由對象,它是一個具有鍵值對結構的對象,可以被多層的嵌套。鍵對對應的URL中傳入的路徑,一般一個鍵值對應依照分割符切割後的某一部分;而鍵值對的值對應的該路徑的需要觸發的回呼函數名稱。回呼函數要在路由表物件使用前先聲明,否則js會報錯。 

另外,回呼函數除非特殊情況,一般不建議使用匿名函數,請盡量先宣告後使用。

  var routes = {
 &#39;/dog&#39;: bark, 
 &#39;/cat&#39;: [meow, scratch]
};
登入後複製

這裡的的url是#dog和#cat 

聲明Router物件後,需要呼叫init()方法進行初始化,如:

router.init();
登入後複製

路由的事件是路由。表中一個有固定命名的屬性,是指當路由方法router.dispatch()被呼叫時,路由匹配成功的時定義的需要觸發的回調方法(允許定義多個回調方法)。上文即時註冊功能裡的"on"方法就是一個事件。具體資訊如下:  

on :當路由配對成功後,需要執行的方法 

before:在觸發「on」方法之前執行的方法 

僅在客戶端有效的方法:在客戶端有效的方法:當離開目前註冊路徑時,需要執行的方法 

once: 目前註冊路徑只執行一次的方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支PHP中文網。


更多用director.js實作前端路由使用實例相關文章請關注PHP中文網!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!