8000 Added readme.md for controller-cancel example · JavaScriptExpert/examples@8e36e2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e36e2e

Browse files
aubergine10aubergine10
aubergine10
authored and
aubergine10
committed
Added readme.md for controller-cancel example
A step-by-step guide to the example.
1 parent 844e22a commit 8e36e2e

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

controller-cancel/definitions.png

7.83 KB
Loading

controller-cancel/readme.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
![Initialisation Sequence](definitions.png)
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

Comments
 (0)
0