Router emits event
This commit is contained in:
parent
6e32bf4e23
commit
e781db308b
@ -1,5 +1,9 @@
|
||||
var Dragonfly = global.Dragonfly;
|
||||
var FatalError = require( '../errors/FatalError.js' );
|
||||
|
||||
var util = require( "util" )
|
||||
, events = require( "events" )
|
||||
, FatalError = require( '../errors/FatalError.js' )
|
||||
;
|
||||
|
||||
var MaxRedirect = 10;
|
||||
|
||||
@ -19,6 +23,8 @@ var RelayPoint = function( uri, internal )
|
||||
|
||||
var Router = function( http )
|
||||
{
|
||||
events.EventEmitter.call( this );
|
||||
|
||||
this.HTTP = http;
|
||||
this.routes = {};
|
||||
this.routable = true;
|
||||
@ -27,11 +33,13 @@ var Router = function( http )
|
||||
// Changed when routing
|
||||
this.inputs = [];
|
||||
this.routeObj = {};
|
||||
this.reroute = false;
|
||||
this.reRoute = false;
|
||||
|
||||
this.relaying = new RelayPoint( http.request.raw.url );
|
||||
};
|
||||
|
||||
util.inherits( Router, events.EventEmitter );
|
||||
|
||||
Router.prototype.addRoute = function( name, _route, _action, _status )
|
||||
{
|
||||
this.routes[ name ] = {
|
||||
@ -97,7 +105,7 @@ Router.prototype.setRoute = function( _route, _match )
|
||||
, inputs: this.inputs
|
||||
|
||||
// Re-route function
|
||||
, reroute: function( target, direct )
|
||||
, reRoute: function( target, direct )
|
||||
{
|
||||
Dragonfly.Log(
|
||||
"Reroute: " + target
|
||||
@ -105,8 +113,10 @@ Router.prototype.setRoute = function( _route, _match )
|
||||
);
|
||||
this.relaying = new RelayPoint( target, direct );
|
||||
this.routable = true;
|
||||
this.reroute = true;
|
||||
this.reRoute = true;
|
||||
this.emit( "Route" );
|
||||
}.bind( this )
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = Router;
|
||||
|
@ -12,6 +12,7 @@ var Framework = function( garden )
|
||||
var Router = require( "./Router" );
|
||||
this.router = new Router( garden );
|
||||
this.router.addRoute( "404", false, "404" );
|
||||
this.router.addListener( "Route", this.parseResult.bind( this ) );
|
||||
|
||||
this.handlers = {
|
||||
"404": function()
|
||||
@ -59,7 +60,7 @@ Framework.prototype.addHandler = function( name, method )
|
||||
|
||||
Framework.prototype.parseResult = function()
|
||||
{
|
||||
while( this.router.routable )
|
||||
if( this.router.routable )
|
||||
{
|
||||
var method = this.router.route();
|
||||
if( method )
|
||||
@ -69,14 +70,14 @@ Framework.prototype.parseResult = function()
|
||||
if( this.handlers[ method ] )
|
||||
{
|
||||
this.handlers[ method ]( this.router.routeObj );
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if( method === false )
|
||||
{
|
||||
Dragonfly.Log( "No route is defined to handle this URI", Dragonfly.Spheres.THERMO );
|
||||
this.router.routeObj.reroute( "404", true );
|
||||
continue;
|
||||
this.router.routeObj.reRoute( "404", true );
|
||||
return;
|
||||
}
|
||||
|
||||
throw new FatalError( "Relay handler \"" + method + "\" is not defined" );
|
||||
|
Loading…
Reference in New Issue
Block a user