Routes

Associate request endpoints to callbacks

Routes create independence between your markup and JavaScript. It also allows us to easily integrate HTML5 History. Wee routing serves as the blueprint for your project's JavaScript.

addFilter

Add conditional route filter

VariableTypeDefaultDescriptionRequired

a

object, string

-

Multiple filter object or filter key

b

function

-

Filter function

Single

Wee.routes.addFilter('isInternal', function(seg) {
    return ['admin', 'protected'].indexOf(seg) > -1;
});

Multiple

Wee.routes.addFilter({
    isInternal: function(seg) {
        return ['admin', 'protected'].indexOf(seg) > -1;
    },
    isExternal: function(seg) {
        return ['admin', 'protected'].indexOf(seg) < 0;
    }
});

Filters

Included route filters

Once

Evaluate the route only once.

Wee.routes.map({
    '$any:once': 'common'
});

Fire

Moves the route to the top of the evaluation order.

Wee.routes.map({
    '$any:fire': 'common'
});

Unload

Evaluates when navigating away from a route with Wee.history. A common use-case for this filter is to manage in-memory objects and unbind page-specific events when navigating away from a page to prevent memory leaks.

Wee.routes.map({
    '$any:unload': 'common:unload'
});

Pop

Evaluates when a history entry is changed. This will be triggered by doing a browser action such as a click on the back or forward button, or by calling history.back() or history.forward().

Wee.routes.map({
    '$any:pop': 'common:pop'
});

Eval

Move the segment pointer back one level.

Wee.routes.map({
    '$customFilter:eval': {
        '$any': 'common',
        'products': 'products'
    }
});

!

Negates route if prefixed by js !.

Wee.routes.map({
    '!about': 'common'
});

Map

Retrieve or add route endpoints to route storage

VariableTypeDefaultDescriptionRequired

a

object

-

Mapping object

init

boolean

false

Immediately evaluate routes

Set

Arguments can be nested as deep as necessary. Pass true as the second argument to immediately evaluate the specified routes from the first argument.

Wee.routes.map({
    '$any': 'common',
    'script': {
        'routes': function() {
            // Current page
        }
    }
});

If using the wee:fn`` format to call an init method you can exclude:init` as it is assumed if no function is provided.

Get

Retrieve all the routes currently in storage.

Wee.routes.map();
{
    "$any": "common",
    "script": {
        "routes": function(){}
    }
}

Advanced

Wee.routes.map({
    '$root': function(val) {
        console.log('root ' + val);
    },
    '$any': function() {
        console.log('root any');
    },
    'category': {
        '$root||$/^P[\\d]+$/': function(val) {
            console.log(val + ' root');
        },
        '$any': function() {
            console.log('category any');
        },
        'mens': function() {
            console.log('mens!');
        },
        '$/^(?!P[\\d]+).*$/': {
            '$root': function(cat) {
                console.log(cat + ' listing');
            },
            '$num': function(id) {
                console.log('product ID is ' + id);
            }
        }
    }
});

Wee.ready(function() {
    Wee.routes.run({
        path: 'category/name/123'
    });
});

Run

Process stored route options with optional config

VariableTypeDefaultDescriptionRequired

options

object

-

Object parameters below

Options Object

VariableTypeDefaultDescriptionRequired

path

string

-

Relative path from root domain

routes

object

-

Mapping object

Without arguments, the currently bound path and routes will be evaluated.

Wee.routes.run();

Advanced

If an object is provided, only the passed routes will be evaluated.

Wee.routes.run({
    routes: {
        '$any': 'common:init',
        'script': {
            'routes': function() {
                // Current page
            }
        }
    },
    path: '/script/routes'
});

Segments

Get all segments or single segment at index

VariableTypeDefaultDescriptionRequired

index

integer

-

Zero-based segment index

Get all

Retrieve segment array from the currently bound path.

Wee.routes.segments();
["script", "routes"]

Get single

Get single segment by defining zero-based index.

Wee.routes.segments(1);
"routes"

URI

Get currently bound URI values or set URI data with a specified string or value object

VariableTypeDefaultDescriptionRequired

value

string, object

-

Object parameters below

Value Object

VariableTypeDefaultDescriptionRequired

hash

string

-

Hash value

path

string

-

Relative path

Query

object

-

Query string object

Get

Based on “https://www.weepower.com/script/routes?success=yes#uri” the following object would be returned.

Wee.routes.uri();
{
    hash: "uri",
    path: "/script/routes",
    query: {
        success: "yes"
    }
}

Set String

The set method accepts either an absolute URL, a relative path, or just a #hash.

Wee.routes.uri('another/page');

Set Object

Only the properties you wish to update should be provided. They will be merged into the current values.

Wee.routes.uri({
    hash: 'uri',
    path: '/script/routes',
    query: {
        success: 'yes'
    }
});

Setting the URI only updates the internal reference. It doesn’t navigate the page.