+0
日增长
5,568
stars
475
forks
155
watchers
133
prs
228
issues
0
releases
README
!Logo
Synopsis
Director is a router. Routing is the process of determining what code to run
when a URL is requested.
Motivation
A routing library that works in both the browser and node.js environments with
as few differences as possible. Simplifies the development of Single Page Apps
and Node.js applications. Dependency free (doesn't require jQuery or Express,
etc).
Status
Features
Client-Side Routing
Server-Side HTTP Routing
Server-Side CLI Routing
Usage
API Documentation
Frequently Asked Questions
Building client-side script
Run the provided CLI script.
bash
./bin/build
Client-side Routing
It simply watches the hash of the URL to determine what to do, for example:
http://foo.com//bar
Client-side routing (aka hash-routing) allows you to specify some information
about the state of the application using the URL. So that when the user visits
a specific URL, the application can be transformed accordingly.
!Hash route
Here is a simple example:
html
A Gentle Introduction
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();
/author
/books
/books/view/1
Director works great with your favorite DOM library, such as jQuery.
html
A Gentle Introduction 2
$('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();
});
Author Name
Book1, Book2, Book3
/author
/books
You can find a browser-specific build of director [here][1] which has all of
the server code stripped away.
Server-Side HTTP Routing
Director handles roTop issues
#25Parameter dictionary support15 comments · 2011-09-05#312Uncaught TypeError: window.onpopstate is not a function11 comments · 2015-07-08#349Not compatible with webpack2 9 comments · 2017-02-02#345Find the new maintainer8 comments · 2016-11-20#256Support reverse routing (URL generation) with named routes8 comments · 2014-09-10#235setRoute(start, length) not implemented7 comments · 2014-06-10#231Calling a route manually7 comments · 2014-05-21#332Cannot initialize router when used via CommonJS6 comments · 2016-05-24