Skip to content

Commit

Permalink
IE support and minor style fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Sep 1, 2014
1 parent 980675e commit de19c32
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions reititin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ var isArray = Array.isArray || function (arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};

var inherits = function (ctor, superCtor) {
function fnName (fn) {
if (Function.prototype.name === undefined) {
var funcNameRegex = /function\s([^(]{1,})\(/;
var results = (funcNameRegex).exec((fn).toString());
return (results && results.length > 1) ? results[1].trim() : "";
} else {
return fn.name;
}
}

function inherits (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
Expand All @@ -17,7 +27,7 @@ var inherits = function (ctor, superCtor) {
configurable: true
}
});
};
}

var Router = function (routes) {
this.names = {};
Expand All @@ -32,8 +42,8 @@ var Router = function (routes) {
if (isArray(routeCallback)) {
routeName = routeCallback[0];
routeCallback = routeCallback[1];
} else if (routeCallback.name.length > 0) {
routeName = routeCallback.name;
} else if (fnName(routeCallback).length > 0) {
routeName = fnName(routeCallback);
} else {
routeName = route;
}
Expand All @@ -43,8 +53,8 @@ var Router = function (routes) {
}
};

Router.prototype._getByName = function (name) {
return this.routes[this.names[name]];
function getByName (router, name) {
return router.routes[router.names[name]];
};

Router.prototype.add = function (name, route, callback) {
Expand Down Expand Up @@ -124,15 +134,15 @@ Router.prototype.match = function (url) {
Router.prototype.route = function (url) {
var match = this.match(url);
if (match !== false) {
this._getByName(match.name).callback(match);
getByName(this, match.name).callback(match);
return match;
} else {
throw "Couldn't match url '" + url + "'.";
}
};

Router.prototype.find = function (name, params, query) {
var rule = this._getByName(name);
var rule = getByName(this, name);
if (!rule) {
rule = this.routes[name] || {};
}
Expand Down

0 comments on commit de19c32

Please sign in to comment.