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