/
application.js
235 lines (185 loc) · 6.55 KB
/
application.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
// ==========================================================================
// Project: Ember - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
// Portions ©2008-2011 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
var get = Ember.get, set = Ember.set;
/**
@class
An Ember.Application instance serves as the namespace in which you define your
application's classes. You can also override the configuration of your
application.
By default, Ember.Application will begin listening for events on the document.
If your application is embedded inside a page, instead of controlling the
entire document, you can specify which DOM element to attach to by setting
the `rootElement` property to a CSS selector.
MyApp = Ember.Application.create({
rootElement: '#my-app'
});
The root of an Ember.Application must not be removed during the course of the
page's lifetime. If you have only a single conceptual application for the
entire page, and are not embedding any third-party Ember applications
in your page, use the default document root for your application.
You only need to specify the root if your page contains multiple instances
of Ember.Application.
@extends Ember.Object
*/
Ember.Application = Ember.Namespace.extend(
/** @scope Ember.Application.prototype */{
/**
The root DOM element of the Application.
Can be specified as DOMElement or a selector string.
@type DOMElement
@default 'body'
*/
rootElement: 'body',
/**
@type Ember.EventDispatcher
@default null
*/
eventDispatcher: null,
/**
@type Object
@default null
*/
customEvents: null,
/** @private */
init: function() {
var eventDispatcher,
rootElement = get(this, 'rootElement');
this._super();
eventDispatcher = Ember.EventDispatcher.create({
rootElement: rootElement
});
set(this, 'eventDispatcher', eventDispatcher);
// jQuery 1.7 doesn't call the ready callback if already ready
if (Ember.$.isReady) {
Ember.run.once(this, this.didBecomeReady);
} else {
var self = this;
Ember.$(document).ready(function() {
Ember.run.once(self, self.didBecomeReady);
});
}
},
/**
Instantiate all controllers currently available on the namespace
and inject them onto a router.
Example:
App.PostsController = Ember.ArrayController.extend();
App.CommentsController = Ember.ArrayController.extend();
var router = Ember.Router.create({
...
});
App.initialize(router);
router.get('postsController') // <App.PostsController:ember1234>
router.get('commentsController') // <App.CommentsController:ember1235>
router.get('postsController.router') // router
*/
initialize: function(router) {
var properties = Ember.A(Ember.keys(this)),
injections = get(this.constructor, 'injections'),
namespace = this, controller, name;
if (!router && Ember.Router.detect(namespace['Router'])) {
router = namespace['Router'].create();
this._createdRouter = router;
}
if (router) {
set(this, 'router', router);
// By default, the router's namespace is the current application.
//
// This allows it to find model classes when a state has a
// route like `/posts/:post_id`. In that case, it would first
// convert `post_id` into `Post`, and then look it up on its
// namespace.
set(router, 'namespace', this);
}
Ember.runLoadHooks('application', this);
injections.forEach(function(injection) {
properties.forEach(function(property) {
injection[1](namespace, router, property);
});
});
if (router && router instanceof Ember.Router) {
this.startRouting(router);
}
},
/** @private */
didBecomeReady: function() {
var eventDispatcher = get(this, 'eventDispatcher'),
customEvents = get(this, 'customEvents');
eventDispatcher.setup(customEvents);
this.ready();
},
/**
@private
If the application has a router, use it to route to the current URL, and
trigger a new call to `route` whenever the URL changes.
*/
startRouting: function(router) {
var location = get(router, 'location'),
rootElement = get(this, 'rootElement'),
applicationController = get(router, 'applicationController');
Ember.assert("ApplicationView and ApplicationController must be defined on your application", (this.ApplicationView && applicationController) );
var applicationView = this.ApplicationView.create({
controller: applicationController
});
this._createdApplicationView = applicationView;
applicationView.appendTo(rootElement);
router.route(location.getURL());
location.onUpdateURL(function(url) {
router.route(url);
});
},
/**
Called when the Application has become ready.
The call will be delayed until the DOM has become ready.
*/
ready: Ember.K,
/** @private */
willDestroy: function() {
get(this, 'eventDispatcher').destroy();
if (this._createdRouter) { this._createdRouter.destroy(); }
if (this._createdApplicationView) { this._createdApplicationView.destroy(); }
},
registerInjection: function(options) {
this.constructor.registerInjection(options);
}
});
Ember.Application.reopenClass({
concatenatedProperties: ['injections'],
injections: Ember.A(),
registerInjection: function(options) {
var injections = get(this, 'injections'),
before = options.before,
name = options.name,
injection = options.injection,
location;
if (before) {
location = injections.find(function(item) {
if (item[0] === before) { return true; }
});
location = injections.indexOf(location);
} else {
location = get(injections, 'length');
}
injections.splice(location, 0, [name, injection]);
}
});
Ember.Application.registerInjection({
name: 'controllers',
injection: function(app, router, property) {
if (!/^[A-Z].*Controller$/.test(property)) { return; }
var name = property.charAt(0).toLowerCase() + property.substr(1),
controllerClass = app[property], controller;
if(!Ember.Object.detect(controllerClass)){ return; }
controller = app[property].create();
router.set(name, controller);
controller.setProperties({
target: router,
controllers: router,
namespace: app
});
}
});