Sha256: e6f9c457396043f03b31783ffda4beac511d5606e29c2c7bb6d13a845cb68620

Contents?: true

Size: 1.19 KB

Versions: 44

Compression:

Stored size: 1.19 KB

Contents

var Router = (function () {

    "use strict";

    var routes = [];

    function addRoute(route, handler) {

        routes.push({parts: route.split('/'), handler: handler});
    }

    function load(route) {
        window.location.hash = route;
    }

    function start() {

        var path = window.location.hash.substr(1),
            parts = path.split('/'),
            partsLength = parts.length;

        for (var i = 0; i < routes.length; i++) {
            var route = routes[i];
            if (route.parts.length === partsLength) {
                var params = [];
                for (var j = 0; j < partsLength; j++) {
                    if (route.parts[j].substr(0, 1) === ':') {
                        params.push(parts[j]);
                    } else if (route.parts[j] !== parts[j]) {
                        break;
                    }
                }
                if (j === partsLength) {
                    route.handler.apply(undefined, params);
                    return;
                }
            }
        }
    }

    window.onhashchange = start;

    return {
        addRoute: addRoute,
        load: load,
        start: start
    };

}());

module.exports = Router;

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
nutella_framework-0.4.19 framework_components/roomcast-channel-creator/src/app/components/Router.js
nutella_framework-0.4.18 framework_components/roomcast-channel-creator/src/app/components/Router.js
nutella_framework-0.4.17 framework_components/roomcast-channel-creator/src/app/components/Router.js
nutella_framework-0.4.16 framework_components/roomcast-channel-creator/src/app/components/Router.js