-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
route.js
438 lines (338 loc) · 11.8 KB
/
route.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
var get = Ember.get, set = Ember.set,
classify = Ember.String.classify,
decamelize = Ember.String.decamelize;
Ember.Route = Ember.Object.extend({
exit: function() {
teardownView(this);
},
/**
Transition into another route. Optionally supply a model for the
route in question. The model will be serialized into the URL
using the `serialize` hook.
@method transitionTo
@param {String} name the name of the route
@param {...Object} models the
*/
transitionTo: function() {
this.transitioned = true;
return this.router.transitionTo.apply(this.router, arguments);
},
/**
Transition into another route while replacing the current URL if
possible. Identical to `transitionTo` in all other respects.
@method replaceWith
@param {String} name the name of the route
@param {...Object} models the
*/
replaceWith: function() {
this.transitioned = true;
return this.router.replaceWith.apply(this.router, arguments);
},
send: function() {
return this.router.send.apply(this.router, arguments);
},
/**
@private
This hook is the entry point for router.js
@method setup
*/
setup: function(context) {
this.currentModel = context;
this.transitioned = false;
this.redirect(context);
if (this.transitioned) { return; }
var controller = this.controllerFor(this.templateName, context);
if (this.setupControllers) {
Ember.deprecate("Ember.Route.setupControllers is deprecated. Please use Ember.Route.setupController(controller, model) instead.");
this.setupControllers(controller, context);
} else {
this.setupController(controller, context);
}
if (this.renderTemplates) {
Ember.deprecate("Ember.Route.renderTemplates is deprecated. Please use Ember.Route.renderTemplate(controller, model) instead.");
this.renderTemplates(context);
} else {
this.renderTemplate(controller, context);
}
},
/**
A hook you can implement to optionally redirect to another route.
If you call `this.transitionTo` from inside of this hook, this route
will not be entered in favor of the other hook.
@method redirect
@param {Object} model the model for this route
*/
redirect: Ember.K,
/**
@private
The hook called by `router.js` to convert parameters into the context
for this handler. The public Ember hook is `model`.
@method deserialize
*/
deserialize: function(params) {
var model = this.model(params);
return this.currentModel = model;
},
/**
A hook you can implement to convert the URL into the model for
this route.
```js
App.Route.map(function(match) {
match("/posts/:post_id").to("post");
});
```
The model for the `post` route is `App.Post.find(params.post_id)`.
By default, if your route has a dynamic segment ending in `_id`:
* The model class is determined from the segment (`post_id`'s
class is `App.Post`)
* The find method is called on the model class with the value of
the dynamic segment.
@method model
@param {Object} params the parameters extracted from the URL
*/
model: function(params) {
var match, name, value;
for (var prop in params) {
if (match = prop.match(/^(.*)_id$/)) {
name = match[1];
value = params[prop];
}
}
if (!name) { return; }
var className = classify(name),
namespace = this.router.namespace,
modelClass = namespace[className];
Ember.assert("You used the dynamic segment " + name + "_id in your router, but " + namespace + "." + className + " did not exist and you did not override your state's `model` hook.", modelClass);
return modelClass.find(value);
},
/**
A hook you can implement to convert the route's model into parameters
for the URL.
```js
App.Route.map(function(match) {
match("/posts/:post_id").to("post");
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
// the server returns `{ id: 12 }`
return jQuery.getJSON("/posts/" + params.post_id);
},
serialize: function(model) {
// this will make the URL `/posts/12`
return { post_id: model.id };
}
});
```
The default `serialize` method inserts the model's `id` into the
route's dynamic segment (in this case, `:post_id`).
This method is called when `transitionTo` is called with a context
in order to populate the URL.
@method serialize
@param {Object} model the route's model
@param {Array} params an Array of parameter names for the current
route (in the example, `['post_id']`.
@return {Object} the serialized parameters
*/
serialize: function(model, params) {
if (params.length !== 1) { return; }
var name = params[0], object = {};
object[name] = get(model, 'id');
return object;
},
/**
A hook you can use to setup the controller for the current route.
This method is called with the controller for the current route and the
model supplied by the `model` hook.
```js
App.Route.map(function(match) {
match("/posts/:post_id").to("post");
});
```
For the `post` route, the controller is `App.PostController`.
By default, the `setupController` hook sets the `content` property of
the controller to the `model`.
If no explicit controller is defined, the route will automatically create
an appropriate controller for the model:
* if the model is an `Ember.Array` (including record arrays from Ember
Data), the controller is an `Ember.ArrayController`.
* otherwise, the controller is an `Ember.ObjectController`.
This means that your template will get a proxy for the model as its
context, and you can act as though the model itself was the context.
@method setupController
*/
setupController: function(controller, model) {
if (controller) {
controller.set('content', model);
}
},
/**
Returns the controller for a particular route.
```js
App.PostRoute = Ember.Route.extend({
setupController: function(controller, post) {
this._super(controller, post);
this.controllerFor('posts').set('currentPost', post);
}
});
```
By default, the controller for `post` is the shared instance of
`App.PostController`.
@method controllerFor
@param {String} name the name of the route
@param {Object} model the model associated with the route (optional)
@return {Ember.Controller}
*/
controllerFor: function(name, model) {
var container = this.router.container,
controller = container.lookup('controller:' + name);
if (!controller) {
model = model || this.modelFor(name);
controller = Ember.generateController(container, name, model);
}
return controller;
},
/**
Returns the current model for a given route.
This is the object returned by the `model` hook of the route
in question.
@method modelFor
@param {String} name the name of the route
@return {Object} the model object
*/
modelFor: function(name) {
return this.container.lookup('route:' + name).currentModel;
},
/**
A hook you can use to render the template for the current route.
This method is called with the controller for the current route and the
model supplied by the `model` hook. By default, it renders the route's
template, configured with the controller for the route.
This method can be overridden to set up and render additional or
alternative templates.
@method renderTemplate
@param {Object} controller the route's controller
@param {Object} model the route's model
*/
renderTemplate: function(controller, model) {
this.render();
},
/**
Renders a template into an outlet.
This method has a number of defaults, based on the name of the
route specified in the router.
For example:
```js
App.Router.map(function(match) {
match("/").to("index");
match("/posts/:post_id").to("post");
});
App.PostRoute = App.Route.extend({
renderTemplate: function() {
this.render();
}
});
```
The name of the `PostRoute`, as defined by the router, is `post`.
By default, render will:
* render the `post` template
* with the `post` view (`PostView`) for event handling, if one exists
* and the `post` controller (`PostController`), if one exists
* into the `main` outlet of the `application` template
You can override this behavior:
```js
App.PostRoute = App.Route.extend({
renderTemplate: function() {
this.render('myPost', { // the template to render
into: 'index', // the template to render into
outlet: 'detail', // the name of the outlet in that template
controller: 'blogPost' // the controller to use for the template
});
}
});
```
Remember that the controller's `content` will be the route's model. In
this case, the default model will be `App.Post.find(params.post_id)`.
@method render
@param {String} name the name of the template to render
@param {Object} options the options
*/
render: function(name, options) {
if (typeof name === 'object' && !options) {
options = name;
name = this.templateName;
}
name = name || this.templateName;
var container = this.container,
view = container.lookup('view:' + name),
template = container.lookup('template:' + name);
if (!view && !template) { return; }
this.lastRenderedTemplate = name;
options = normalizeOptions(this, name, template, options);
view = setupView(view, container, options);
appendView(this, view, options);
}
});
function parentRoute(route) {
var handlerInfos = route.router.router.currentHandlerInfos;
var parent, current;
for (var i=0, l=handlerInfos.length; i<l; i++) {
current = handlerInfos[i].handler;
if (current === route) { return parent; }
parent = current;
}
}
function parentTemplate(route) {
var parent = parentRoute(route), template;
if (!parent) { return; }
if (template = parent.lastRenderedTemplate) {
return template;
} else {
return parentTemplate(parent);
}
}
function normalizeOptions(route, name, template, options) {
options = options || {};
options.into = options.into || parentTemplate(route);
options.outlet = options.outlet || 'main';
options.name = name;
options.template = template;
var controller = options.controller || route.templateName;
if (typeof controller === 'string') {
controller = route.container.lookup('controller:' + controller);
}
options.controller = controller;
return options;
}
function setupView(view, container, options) {
var defaultView = options.into ? 'view:default' : 'view:toplevel';
view = view || container.lookup(defaultView);
if (!get(view, 'templateName')) {
set(view, 'template', options.template);
}
set(view, 'renderedName', options.name);
set(view, 'controller', options.controller);
return view;
}
function appendView(route, view, options) {
if (options.into) {
var parentView = route.router._lookupActiveView(options.into);
route.teardownView = teardownOutlet(parentView, options.outlet);
parentView.connectOutlet(options.outlet, view);
} else {
var rootElement = get(route, 'router.namespace.rootElement');
route.router._connectActiveView(options.name, view);
route.teardownView = teardownTopLevel(view);
view.appendTo(rootElement);
}
}
function teardownTopLevel(view) {
return function() { view.remove(); };
}
function teardownOutlet(parentView, outlet) {
return function() { parentView.disconnectOutlet(outlet); };
}
function teardownView(route) {
if (route.teardownView) { route.teardownView(); }
delete route.teardownView;
delete route.lastRenderedTemplate;
}