forked from simme/ampersand-controller-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
70 lines (63 loc) · 1.83 KB
/
router.js
File metadata and controls
70 lines (63 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// # Ampersand Controller Router
//
/* jshint browserify: true */
'use strict';
var AmpRouter = require('ampersand-router');
var defaults = require('amp-defaults');
module.exports = AmpRouter.extend({
props: {
controllers: 'object',
controllerOptions: 'object',
routeCallback: 'function',
},
//
// ## Initialize
//
// * **options** object of options:
// * **controllers**, object of controllers. The key should be the name of
// the controller (as used in the routes) and the value should be a
// controller _constructor_.
// * **controllerOptions**, optional options passed to controller
// constructor.
// * **routeCallback**, passed as the last argument to the route action.
//
initialize: function initRouter(options) {
this.controllers = options.controllers || {};
this.controllerOptions = options.controllerOptions || {};
this.routeCallback = options.routeCallback || function () {};
},
//
// ## Start the Router
//
// Will start the router. This will parse the _current_ URL and start
// monitoring the URL for changes.
//
// Options are the same as for the Ampersand router.
//
start: function (opts) {
this.history.start(defaults(opts || {}, {
pushState: true,
hashChange: false,
silent: false,
root: '/',
}));
},
//
// ## Execute
//
// Internal function for handling the calling of the controller.
//
execute: function (cb, args, name) {
var parts = name.split('.');
if (parts.length !== 2) {
throw new Error('Invalid route definition: ' + name);
}
var controller = new (this.controllers[parts[0]])(this.controllerOptions);
var action = controller[parts[1]];
args.push(this.routeCallback);
if (typeof action === 'function') {
action.apply(controller, args);
}
},
});