mixdown-router
Advanced tools
Comparing version 0.2.9 to 1.0.0
212
index.js
@@ -7,91 +7,179 @@ var _ = require('lodash'); | ||
var Generator = require('./lib/generator.js'); | ||
var MockRequest = require('hammock').Request; | ||
var MockResponse = require('hammock').Response; | ||
var Router = function() { | ||
this.name = 'router'; | ||
}; | ||
var Router = function(namespace) { | ||
namespace = namespace || 'router'; | ||
var instance = this; | ||
Router.prototype._baseHandler = function() { | ||
var handler = arguments[0]; | ||
var route = arguments[1]; | ||
var httpContext = arguments[2]; | ||
/** | ||
* Attaches a router plugin to an application. | ||
* | ||
**/ | ||
this.attach = function (options) { | ||
var app = options.app; | ||
var handlers = options.handlers || instance; | ||
var context = _.clone(httpContext); | ||
context.app = this; | ||
context.route = route; | ||
// attach the generator part of the router. | ||
app.plugins.use(new Generator(namespace), options); | ||
handler.call(this, context); | ||
}; | ||
var self = this[namespace]; | ||
var _clientRouter = null; | ||
// attach the server side component | ||
_.extend(self, { | ||
/** | ||
* Attaches a router plugin to an application. | ||
* | ||
**/ | ||
Router.prototype.attach = function (options) { | ||
var app = options.app; | ||
var handlers = options.handlers || this.plugins.router; | ||
getHandler: function(key) { | ||
// check object first. | ||
// then check prototype. | ||
return _.isFunction(handlers[key]) ? handlers[key] : handlers.constructor.prototype[key]; | ||
}, | ||
// attached the router. | ||
Generator.prototype.attach.call(this, options); | ||
/** | ||
* Creates a new instance of router. | ||
* | ||
**/ | ||
create: function() { | ||
var newRouter = new plRouter(); | ||
var router = this.router; | ||
// attach the server side component | ||
_.extend(this.router, { | ||
if (options.timeout) { | ||
newRouter.timeout = options.timeout; | ||
} | ||
var addParam = function (param, key) { | ||
/** | ||
* Creates a new instance of router. | ||
* | ||
**/ | ||
create: function() { | ||
var newRouter = new plRouter(); | ||
if (param && param.regex) { | ||
if (options.timeout) { | ||
newRouter.timeout = options.timeout; | ||
} | ||
var addParam = function (param, key) { | ||
if (param.kind.toLowerCase() === "rest") { | ||
newRouter.param(key, new RegExp(param.regex)); | ||
} | ||
else if (param.kind.toLowerCase() === 'query') { | ||
newRouter.qparam(key, new RegExp(param.regex)); | ||
} | ||
} | ||
}; | ||
if (param && param.regex) { | ||
// add routes | ||
_.each(self.routes, function (route, key) { | ||
if (param.kind.toLowerCase() === "rest") { | ||
newRouter.param(key, new RegExp(param.regex)); | ||
// add route-level params | ||
if (route.params) { | ||
_.each(route.params, addParam); | ||
} | ||
else if (param.kind.toLowerCase() === 'query') { | ||
newRouter.qparam(key, new RegExp(param.regex)); | ||
// check object first. | ||
var handler = self.getHandler(route.handler); | ||
// if we found the handler on the router, then bind it. | ||
if (_.isFunction(handler)) { | ||
newRouter.use( | ||
route.method, | ||
route.path, | ||
{ timeout: route.timeout }, | ||
handlers.constructor.prototype._baseHandler.bind(app, handler, route) | ||
); | ||
} | ||
} | ||
}; | ||
// add routes | ||
_.each(router.routes, function (route, key) { | ||
}); | ||
// add route-level params | ||
if (route.params) { | ||
_.each(route.params, addParam); | ||
return newRouter; | ||
}, | ||
listen: function(callback) { | ||
self.navigate(url.format(window.location), callback); | ||
}, | ||
// client side url navigation | ||
// 2 overloaded options | ||
// | ||
// 1. function(route, params, callback) | ||
// @param route {String}: named route to generate the url. | ||
// @param params {Object}: hash of params to send to url generation. | ||
// | ||
// 2. function(url, callback) | ||
// @param url {Object|String}: Can be url string or node url object. | ||
navigate: function(route, params, callback) { | ||
var newUrl = null; | ||
// handle 2 arg variant function signatures. | ||
if (arguments.length === 2) { | ||
var arg1 = arguments[1]; | ||
if (_.isFunction(arg1)) { | ||
callback = arg1; | ||
params = null; | ||
} | ||
else { | ||
callback = null; | ||
params = arg1; | ||
} | ||
} | ||
var handler = handlers[route.handler]; | ||
// if the route is in the route table, then generate the url. if not, then this is a literal url. | ||
if (self.routes[route]) { | ||
newUrl = app.plugins.router.format(route, params); | ||
} | ||
else { | ||
newUrl = route; | ||
} | ||
if (!_.isFunction(handler)) { | ||
handler = handlers.constructor.prototype[route.handler]; | ||
if (!history.pushState) { | ||
window.location.href = newUrl; | ||
return; | ||
} | ||
if (_.isFunction(handler)) { | ||
newRouter.use( | ||
route.method, | ||
route.path, | ||
{ timeout: route.timeout }, | ||
handlers.constructor.prototype._baseHandler.bind(app, handler, route) | ||
); | ||
// keep a single instance around in a browser. | ||
if (!_clientRouter) { | ||
_clientRouter = self.create(); | ||
} | ||
}); | ||
var req = new MockRequest({ url: newUrl }); | ||
var res = new MockResponse(); | ||
return newRouter; | ||
} | ||
// if the route was matched, then change the url. This will change the url in the address bar before the handler runs. | ||
// This is good for devs for the situation where there is a problem with the controller handler which will cause the pipeline to stop. | ||
_clientRouter.once('match', function(routerData) { | ||
var httpContext = routerData.httpContext; | ||
if (httpContext.url.href !== window.location.href) { | ||
history.pushState({}, httpContext.url.pathname, httpContext.url.href); | ||
} | ||
}); | ||
}); | ||
// fire callback once the handler has executed. Note: javascript is async. The handler might not be done when this callback is fired... but you already knew that! | ||
_clientRouter.once('end', function(err, results) { | ||
_.isFunction(callback) ? callback(err, { | ||
matched: results[0].matched, | ||
res: res, | ||
req: req | ||
}) : null; | ||
}); | ||
// fire async so that the req is pass to calling context. | ||
// setTimeout(_clientRouter.dispatch.bind(_clientRouter, req, res), 5); | ||
_clientRouter.dispatch(req, res); | ||
return req; | ||
} | ||
}); | ||
}; | ||
}; | ||
Router.prototype._baseHandler = function() { | ||
var handler = arguments[0]; | ||
var route = arguments[1]; | ||
var httpContext = arguments[2]; | ||
var context = _.clone(httpContext); | ||
context.app = this; | ||
context.route = route; | ||
handler.call(this, context); | ||
}; | ||
module.exports = Router; |
{ | ||
"name": "mixdown-router", | ||
"version": "0.2.9", | ||
"version": "1.0.0", | ||
"description": "Router for mixdown.js", | ||
@@ -21,9 +21,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "1.2.1", | ||
"pipeline-router": "1.0.x" | ||
"lodash": "~2.2.0", | ||
"pipeline-router": "~1.1.0", | ||
"hammock": "~0.1.1" | ||
}, | ||
"devDependencies": { | ||
"tape": "*", | ||
"mixdown-server": "0.3.x", | ||
"hammock": "0.1.x" | ||
"mixdown-server": "0.3.x" | ||
}, | ||
@@ -30,0 +30,0 @@ "author": "Tommy at Vast dot com", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
30641
2
606
1
3
+ Addedhammock@~0.1.1
+ Addedcookies@0.3.8(transitive)
+ Addedguid@0.0.11(transitive)
+ Addedhammock@0.1.10(transitive)
+ Addedlodash@2.2.12.4.2(transitive)
+ Addednode-pipeline@1.0.2(transitive)
+ Addedpipeline-router@1.1.8(transitive)
- Removedlodash@1.2.1(transitive)
- Removednode-pipeline@0.5.2(transitive)
- Removedpipeline-router@1.0.4(transitive)
Updatedlodash@~2.2.0
Updatedpipeline-router@~1.1.0