forked from jashkenas/backbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
96 lines (78 loc) · 2.36 KB
/
controller.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$(document).ready(function() {
module("Backbone.Controller");
var Controller = Backbone.Controller.extend({
routes: {
"search/:query": "search",
"search/:query/p:page": "search",
"splat/*args/end": "splat",
"*first/complex-:part/*rest": "complex",
":entity?*args": "query"
},
initialize : function(options) {
this.testing = options.testing;
},
search : function(query, page) {
this.query = query;
this.page = page;
},
splat : function(args) {
this.args = args;
},
complex : function(first, part, rest) {
this.first = first;
this.part = part;
this.rest = rest;
},
query : function(entity, args) {
this.entity = entity;
this.queryArgs = args;
}
});
var controller = new Controller({testing: 101});
Backbone.history.interval = 9;
Backbone.history.start();
test("Controller: initialize", function() {
equals(controller.testing, 101);
});
asyncTest("Controller: routes (simple)", 2, function() {
window.location.hash = 'search/news';
setTimeout(function() {
equals(controller.query, 'news');
equals(controller.page, undefined);
start();
}, 10);
});
asyncTest("Controller: routes (two part)", 2, function() {
window.location.hash = 'search/nyc/p10';
setTimeout(function() {
equals(controller.query, 'nyc');
equals(controller.page, '10');
start();
}, 10);
});
asyncTest("Controller: routes (splats)", function() {
window.location.hash = 'splat/long-list/of/splatted_99args/end';
setTimeout(function() {
equals(controller.args, 'long-list/of/splatted_99args');
start();
}, 10);
});
asyncTest("Controller: routes (complex)", 3, function() {
window.location.hash = 'one/two/three/complex-part/four/five/six/seven';
setTimeout(function() {
equals(controller.first, 'one/two/three');
equals(controller.part, 'part');
equals(controller.rest, 'four/five/six/seven');
start();
}, 10);
});
asyncTest("Controller: routes (query)", 2, function() {
window.location.hash = 'mandel?a=b&c=d';
setTimeout(function() {
equals(controller.entity, 'mandel');
equals(controller.queryArgs, 'a=b&c=d');
start();
window.location.hash = '';
}, 10);
});
});