1
1
/* jshint node: true */
2
2
'use strict' ;
3
3
4
- var NoActionException = require ( './exceptions/NoAction' ) ,
5
- Class = require ( 'uberclass' ) ;
4
+ var NoActionException = require ( './exceptions/NoAction' )
5
+ , Class = require ( 'uberclass' )
6
+ , path = require ( 'path' ) ;
6
7
7
8
module . exports = Class . extend (
8
9
/* @Static */
9
10
{
10
- actionsEnabled : true ,
11
+ route : false ,
11
12
12
- httpMethodsEnabled : true ,
13
+ autoRouting : true ,
14
+
15
+ actionRouting : true ,
16
+
17
+ restfulRouting : true ,
13
18
14
19
attach : function ( ) {
15
- return this . callback ( 'newInstance' ) ;
20
+ return this . callback ( 'newInstance' ) ;
21
+ } ,
22
+
23
+ setup : function ( ) {
24
+ var self = this ;
25
+ if ( typeof injector !== 'undefined' && this . autoRouting !== false && this . route !== false ) {
26
+ injector . inject ( function ( app ) {
27
+ self . autoRoute ( app ) ;
28
+ } ) ;
29
+ }
30
+ } ,
31
+
32
+ autoRoute : function ( app ) {
33
+ var middleware = [ ] ;
34
+
35
+ if ( this . autoRouting instanceof Array ) {
36
+ this . autoRouting . forEach ( function ( mw ) {
37
+ middleware . push ( typeof mw === 'string' ? this . callback ( mw ) : mw ) ;
38
+ } . bind ( this ) ) ;
39
+ }
40
+
41
+ middleware . push ( this . attach ( ) ) ;
42
+
43
+ app . all . apply ( app , [ [ this . route , ':action' , ':id?' ] . join ( '/' ) ] . concat ( middleware ) ) ; // /example/:action/:id?
44
+ app . all . apply ( app , [ [ this . route , ':action?' ] . join ( '/' ) ] . concat ( middleware ) ) ; // /example/?:action?
45
+ } ,
46
+
47
+ extend : function ( ) {
48
+ var extendingArgs = [ ] . slice . call ( arguments )
49
+ , stack = new Error ( ) . stack . split ( '\n' )
50
+ , stack = stack . splice ( 1 , stack . length - 1 )
51
+ , extendingFilePath = false
52
+ , extendingFileName = false
53
+ , route = null ;
54
+
55
+ while ( stack . length > 0 && extendingFilePath === false ) {
56
+ var file = stack . shift ( ) ;
57
+ if ( ! / c l e v e r - c o n t r o l l e r / ig. test ( file ) && ! / u b e r c l a s s / ig. test ( file ) ) {
58
+ if ( / \( ( [ ^ \[ \: ] + ) .* \) / ig. test ( file ) ) {
59
+ extendingFilePath = RegExp . $1 ;
60
+ extendingFileName = path . basename ( extendingFilePath ) . replace ( / ( c o n t r o l l e r ) ? .j s / ig, '' ) . toLowerCase ( ) ;
61
+ }
62
+ }
63
+ }
64
+
65
+ if ( [ '' , 'controller' ] . indexOf ( extendingFileName ) === - 1 && this . route === false ) {
66
+ route = [ '/' , extendingFileName ] . join ( '' ) ;
67
+ // debug( 'Binding automatic route name??' )
68
+ if ( extendingArgs . length === 2 ) {
69
+ extendingArgs [ 0 ] . route = route ;
70
+ } else {
71
+ extendingArgs . unshift ( { route : route } ) ;
72
+ }
73
+ }
74
+
75
+ return this . _super . apply ( this , extendingArgs ) ;
16
76
}
17
77
} ,
18
78
/* @Prototype */
@@ -40,7 +100,7 @@ module.exports = Class.extend(
40
100
this . res = res ;
41
101
42
102
// Override routes where you attach specifically to a single route
43
- if ( this . Class . actionsEnabled && / \/ / . test ( this . req . url ) ) {
103
+ if ( this . Class . actionRouting && / \/ / . test ( this . req . url ) ) {
44
104
var parts = this . req . url . split ( '/' ) ;
45
105
funcName = parts [ parts . length - 1 ] ;
46
106
@@ -57,7 +117,7 @@ module.exports = Class.extend(
57
117
}
58
118
59
119
// Route based on an action first if we can
60
- if ( this . Class . actionsEnabled && typeof this . req . params !== 'undefined' && typeof this . req . params . action !== 'undefined' ) {
120
+ if ( this . Class . actionRouting && typeof this . req . params !== 'undefined' && typeof this . req . params . action !== 'undefined' ) {
61
121
// Action Defined Routing
62
122
if ( isNaN ( this . req . params . action ) ) {
63
123
funcName = this . req . params . action + 'Action' ;
@@ -83,7 +143,7 @@ module.exports = Class.extend(
83
143
}
84
144
85
145
// Route based on the HTTP Method, otherwise throw an exception
86
- if ( this . Class . httpMethodsEnabled ) {
146
+ if ( this . Class . restfulRouting ) {
87
147
if ( this . isGet ( ) && ( this . req . params === undefined || this . req . params . id === undefined ) && typeof this . listAction === 'function' ) {
88
148
method = 'listAction' ;
89
149
} else {
0 commit comments