Router

constructor
 Router() 

Option name Type Description
routes array

List of routers

defaultRoute object

Default route

controller string

The name of the related controller

owl.Router

function Router(routes, defaultRoute, controller){
    var that = this;
    this.routes = [];
    this.defaultRoute = defaultRoute || ({
            callback: function() {
                console.log('Default route is not defined');
            }
        });
    this.controller = controller || null;

    if (routes instanceof Array) {
        routes.forEach(function(route) {
            that.addRoute(route);
        });
    }
}
Router.prototype = {

open

method
 open() 

Option name Type Description
path string

Page path

return Promise.<function|null>

Function to destroy controller

Opens page by path

open: function(path) {
    var route = this.getRoute(path), that = this;
    if (!route) {
        return owl.Promise.resolve(null);
    }

    return this.resolve(route).then(function (resolveResult) {
        return that.run(path, route, resolveResult);
    }).catch(function (e) {
        console.error('Error in Router.open', e.message, e.stack);
        return that.run(path, that.defaultRoute, e);
    });
},

addRoute

method
 addRoute() 

Option name Type Description
route object

Route to add

Adds a route

addRoute: function(route) {
    var paramRegexp = /:[a-zA-Z0-9]*/g,
        pattern = route.path.replace(paramRegexp, '([^/]*)'),
        match = route.path.match(paramRegexp),
        params = {};
    if (match) {
        route.regexp = new RegExp('^' + pattern + '$');
        params = match.map(function(param) {
            return param.substring(1);
        });
    }
    route.params = params;
    this.routes.push(route);
},

getRoute

method
 getRoute() 

Option name Type Description
path string

Path

return object

Returns the route by path

getRoute: function(path) {
    var that = this,
        route;
    this.routes.some(function(currentRoute) {
        var test = currentRoute.regexp ? currentRoute.regexp.test(path) : currentRoute.path === path;
        if(test) {
            route = currentRoute;
            return true;
        }
        return false;
    });
    if (route) {
        return route;
    } else {
        return this.defaultRoute;
    }
},

setDefaultRoute

method
 setDefaultRoute() 

Option name Type Description
route object

Route

Sets default route

setDefaultRoute: function(route) {
    this.defaultRoute = route;
},

getDefaultRoute

method
 getDefaultRoute() 

Option name Type Description
return object

Gets default route

getDefaultRoute: function() {
    return this.defaultRoute;
},

setController

method
 setController() 

Option name Type Description
controller Object

Related controller

Sets controller

setController: function(controller) {
    this.controller = controller;
},

getController

method
 getController() 

Option name Type Description
return string

Related controller

Gets controller

getController: function() {
    return this.controller;
}
    };
    owl.Router = Router;
})(window, owl);