Skip to content

Commit

Permalink
Minor enhancements, including changes to this.extend.
Browse files Browse the repository at this point in the history
No API change occurs with this commit.
  • Loading branch information
Southern authored and jfhbrook committed Sep 29, 2012
1 parent 3b5880f commit 09602f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
12 changes: 6 additions & 6 deletions lib/director/http/index.js
Expand Up @@ -22,15 +22,15 @@ var Router = exports.Router = function (routes) {
//
// ### Extend the `Router` prototype with all of the RFC methods.
//
this.params = {};
this.routes = {};
this.methods = ['on', 'after', 'before'];
this.scope = [];
this._methods = {};
this.recurse = 'forward';
this._attach = [];

this.extend(exports.methods.concat(['before', 'after']));
//
// ### Inherit properties from the `director.Router` constructor.
//
director.Router.apply(this);

this.extend(exports.methods);
this.configure();
this.mount(routes || {});
};
Expand Down
41 changes: 30 additions & 11 deletions lib/director/router.js
Expand Up @@ -172,9 +172,7 @@ var Router = exports.Router = function (routes) {
Router.prototype.configure = function (options) {
options = options || {};

for (var i = 0; i < this.methods.length; i++) {
this._methods[this.methods[i]] = true;
}
this.extend(this.methods);

this.recurse = options.recurse || this.recurse || false;
this.async = options.async || false;
Expand Down Expand Up @@ -710,24 +708,45 @@ Router.prototype.insert = function (method, path, route, parent) {
// for each of the specified `methods`
//
Router.prototype.extend = function(methods) {
var self = this,
len = methods.length,
i;
var self = this;

function extend(method) {
self._methods[method] = true;
self[method] = function () {

//
// ### Function to be used in our methods.
//
function route() {
var extra = arguments.length === 1
? [method, '']
: [method];

self.on.apply(self, extra.concat(Array.prototype.slice.call(arguments)));
};
}
}

for (i = 0; i < len; i++) {
extend(methods[i]);
//
// ### Set the method if it's not listed as a default in `this.methods`.
//
if (!~self.methods.indexOf(method)) {
//
// ### Method has already been defined.
//
if (self._methods[method]) {
//
// ### Method is our route function. No action required.
//
if (self[method] === route) return;
}
//
// ### If all is good, set the method to our route function.
//
self[method] = route;
}
}

methods.forEach(function(method) {
extend(method);
});
};

//
Expand Down

0 comments on commit 09602f3

Please sign in to comment.