Director Javascript Library (Document! X Sample)
(Global) Namespace / Router type
In This Topic
    Router type
    In This Topic
    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.
    Syntax
    var instance = new Router();
    function Router;
    Example
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>A Gentle Introduction</title>
        <script src="https://raw.github.com/flatiron/director/master/build/director.min.js"></script>
        <script>
    
          var author = function () { console.log("author"); },
              books = function () { console.log("books"); },
              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>
    Inheritance Hierarchy

    Object
       Router

    Browser Compatibility
    8
    5
    5
    Constructors
     NameDescription
    public Constructor  
    Top
    Fields
     NameDescription
    public FieldControls async routing. Use true or false. Default is false.  
    public FieldCharacter separator between route fragments. Default is /.  
    public FieldA function (or list of functions) to call on every call to router.dispatch() when a route is found.  
    public FieldHistory object.  
    public FieldIf set to true and client supports pushState(), then uses HTML5 History API instead of hash fragments. See History API for more information.  
    public FieldA function to call if no route is found on a call to router.dispatch().  
    public FieldURL Params.  
    public FieldControls route recursion. Use forward, backward, or false. Default is false Client-side, and backward Server-side.  
    public FieldAn object to which string-based routes will be bound. This can be especially useful for late-binding to route functions (such as async client-side requires).  
    public FieldSet of routes.  
    public FieldIf html5history is enabled, the route handler by default is executed upon Router.init() since with real URIs the router can not know if it should call a route handler or not. Setting this to false disables the route handler initial execution.  
    public FieldRoute scope information  
    public FieldIf set to false, then trailing slashes (or other delimiters) are allowed in routes. Default is true.  
    Top
    Methods
     NameDescription
    public MethodGiven the flexible nature of director there are several options available for both the Client-side and Server-side. These options can be set using the .configure() method  
    public MethodDispatches the route handlers matched within the Routing Table for this instance for the specified method and path.  
    public MethodReturns the entire route or just a section of it.  
    public MethodInitialize the router, start listening for changes to the URL.  
    public Methodinsert a callback that will only occur once per the matched route.  
    public MethodInserts the partial Routing Table, routes, into the Routing Table for this Router instance at the specified path.  
    public MethodAdds the route handler for the specified method and path within the Routing Table.  
    public MethodAdds a route fragment for the given string token to the specified regex matcher to this Router instance. See URL Parameters for more documentation.  
    public MethodRemove a segment from the current route.  
    Top
    See Also