Documentation

Plugins/Claypool/router

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Plugins/Claypool

router( id, options )

Creates a new low level router.
Routers are the heart of jquery.claypool. They provide clear, simple hijax patterns that sit between the browser and your application allowing all relevant events to be registered to a handful of routers that can delegate the events based on powerful regular expressions to your dormant application controllers using common or custom strategies, like first(tcp) and all(udp). Routers are a well known and popular framework tool to simplify application development.
Arguments:
idString
A unique name for this router.
optionsObject
A hash of router options.

Options:
NameType
selectorString
An optional jQuery selector, if none is present the router is bound to the document
eventString
required for all but a pure event router, the event specifies the type of event the router binds to (eg 'click')
stratgeyStringDefault: 'all'
Currently supports 'first' and 'all'
routerKeysString
A pipe delimited list of properties that will be checked in the router configuration to match against the target
hijaxKeyString
a human readable name used to log the hijax event
eventNamespaceString
An event namespace so the router can be cleanly unbound without interfering with other event bindings
targetFunction
This is the delegation function so you can bind a router to, for example, a table but return the delegated target (eg a td)


Examples:
This example illustrates the use of the router plugin by showing how jquery.claypool defines it's built in routers. See routes.js for more information on how to use routers.

$.router( "hijax:a", {
        selector        : 'a',
        event           : 'click',
        strategy        : 'first',
        routerKeys      : 'urls',
        hijaxKey        : 'link',
        eventNamespace  : "Claypool:MVC:HijaxLinkController",
        target       : function(event){ 
            var link = event.target||event.currentTarget;
            while(link.tagName.toUpperCase()!='A'){
                link = $(link).parent()[0];
            }
            return $(link).attr("href");
        }
    }).router( "hijax:button",{
        selector        : ':button',
        event           : 'click',
        strategy        : 'all',
        routerKeys      : 'urls',
        hijaxKey        : 'button',
        eventNamespace  : "Claypool:MVC:HijaxButtonController",
        target       : function(event){ 
            return event.target.value;
        }
    }).router( "hijax:input",{
        selector        : 'input',
        event           : 'click',
        strategy        : 'all',
        routerKeys      : 'urls',
        hijaxKey        : 'input',
        eventNamespace  : "Claypool:MVC:HijaxInputController",
        target       : function(event){ 
            return event.target.name;
        }
    }).router( "hijax:form",{
        selector        : 'form',
        event           : 'submit',
        strategy        : 'first',
        routerKeys      : 'urls',
        hijaxKey        : 'form',
        eventNamespace  : "Claypool:MVC:HijaxFormController",
        target       : function(event){ 
            return event.target.action;
        }
    }).router( "hijax:event",{
        strategy        : 'all',
        routerKeys      : 'event',
        hijaxKey        : 'event',
        eventNamespace  : "Claypool:MVC:HijaxEventController",
        target       : function(event){ 
            return event.type;
        }
    });

NameType