|
| 1 | +## Example: Controller Cancel |
| 2 | + |
| 3 | +This example shows how to use a framework event to intercept, cancel and redirect controller routing. |
| 4 | + |
| 5 | +Features covered by this example: |
| 6 | + |
| 7 | +* Controllers - route URL requests to code |
| 8 | +* Definitions - coded config files |
| 9 | +* `F.route()` - define a route |
| 10 | +* `F.on('controller')` - intercept controller requests |
| 11 | +* `controller.cancel()` - cancel a request |
| 12 | +* `controller.redirect()` - redirect a request |
| 13 | +* `controller.url` - determine request path |
| 14 | +* `controller.transfer()` - transfer a request |
| 15 | +* `controller.isTransfer` - detect a transfer |
| 16 | + |
| 17 | +### Routing |
| 18 | + |
| 19 | +The controller (`/controllers/default.js`) defines two routes: |
| 20 | + |
| 21 | +```javascript |
| 22 | +F.route( '/' , view_index ); |
| 23 | +F.route( '/cancel/', view_cancel ); |
| 24 | +``` |
| 25 | + |
| 26 | +We're going to make `/` route to `view_cancel` instead of `view_index`. The hard way (: |
| 27 | + |
| 28 | +### Redirecting |
| 29 | + |
| 30 | +We'll put our code in a definition file (`/definitions/cancel.js`) - definitions are initialised just before controllers as shown below: |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +First, we need to listen to the framework event for controllers: `F.on('controller')`: |
| 35 | + |
| 36 | +```javascript |
| 37 | +F.on( 'controller', function( controller, name ) { |
| 38 | + |
| 39 | + // this will be triggered on every request to the controller |
| 40 | + |
| 41 | +} |
| 42 | +``` |
| 43 | +
|
| 44 | +The most simplistic (and disastrous) way to redirect to `/cancel/` is as follows: |
| 45 | +
|
| 46 | +```javascript |
| 47 | +F.on( 'controller', function( controller, name ) { |
| 48 | + |
| 49 | + controller.cancel().redirect('/cancel/'); |
| 50 | + |
| 51 | +} |
| 52 | +``` |
| 53 | +
|
| 54 | +This cancels any request in the controller, regardless of route, and then redirects to `/cancel/` ...which is itself a request and so it too will be intercepted, cancelled and redirected to `/cancel/`... over and over again. |
| 55 | +
|
| 56 | +Let's fix that: |
| 57 | +
|
| 58 | +```javascript |
| 59 | +F.on( 'controller', function( controller, name ) { |
| 60 | + |
| 61 | + if (controller.url === '/') |
| 62 | + controller.cancel().redirect('/cancel/'); |
| 63 | + |
| 64 | +} |
| 65 | +``` |
| 66 | +
|
| 67 | +Now, only requests to `/` will be redirected to `/cancel/`, and requests to `/cancel/` will be successful. |
| 68 | +
|
| 69 | +## Transferring |
| 70 | +
|
| 71 | +At first glance, transfers work just the same as redirects: |
| 72 | +
|
| 73 | +```javascript |
| 74 | +F.on( 'controller', function( controller, name ) { |
| 75 | + |
| 76 | + if ( controller.url === '/' ) |
| 77 | + controller.cancel().transfer( '/cancel/' ); |
| 78 | + |
| 79 | +} |
| 80 | +``` |
| 81 | +
|
| 82 | +However, there's a major benefit to using `.transfer()`: You can identify them using `.isTransfer`. |
| 83 | +
|
| 84 | +Let's say you wanted to temporarilly cancel any route in your controller, for example due to site maintenance. You could do something like this: |
| 85 | +
|
| 86 | +
|
| 87 | +```javascript |
| 88 | +F.on( 'controller', function( controller, name ) { |
| 89 | + |
| 90 | + if ( !controller.isTransfer ) // <-- exclude existing transfers |
| 91 | + controller.cancel().transfer( '/cancel/' ); |
| 92 | + |
| 93 | +} |
| 94 | +``` |
0 commit comments