/
core_object.js
272 lines (203 loc) · 7.14 KB
/
core_object.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
// ==========================================================================
// Project: Ember Runtime
// Copyright: ©2011 Strobe Inc. and contributors.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
// NOTE: this object should never be included directly. Instead use Ember.
// Ember.Object. We only define this separately so that Ember.Set can depend on it
var rewatch = Ember.rewatch;
var classToString = Ember.Mixin.prototype.toString;
var set = Ember.set, get = Ember.get;
var o_create = Ember.platform.create,
o_defineProperty = Ember.platform.defineProperty,
a_slice = Array.prototype.slice,
meta = Ember.meta;
/** @private */
function makeCtor() {
// Note: avoid accessing any properties on the object since it makes the
// method a lot faster. This is glue code so we want it to be as fast as
// possible.
var wasApplied = false, initMixins, init = false, hasChains = false;
var Class = function() {
if (!wasApplied) { Class.proto(); } // prepare prototype...
if (initMixins) {
this.reopen.apply(this, initMixins);
initMixins = null;
rewatch(this); // always rewatch just in case
this.init.apply(this, arguments);
} else {
if (hasChains) {
rewatch(this);
} else {
Ember.GUID_DESC.value = undefined;
o_defineProperty(this, Ember.GUID_KEY, Ember.GUID_DESC);
}
if (init===false) { init = this.init; } // cache for later instantiations
Ember.GUID_DESC.value = undefined;
o_defineProperty(this, '_super', Ember.GUID_DESC);
init.apply(this, arguments);
}
};
Class.toString = classToString;
Class.willReopen = function() {
if (wasApplied) {
Class.PrototypeMixin = Ember.Mixin.create(Class.PrototypeMixin);
}
wasApplied = false;
};
Class._initMixins = function(args) { initMixins = args; };
Class.proto = function() {
var superclass = Class.superclass;
if (superclass) { superclass.proto(); }
if (!wasApplied) {
wasApplied = true;
Class.PrototypeMixin.applyPartial(Class.prototype);
Ember.rewatch(Class.prototype); // setup watch chains if needed.
hasChains = !!meta(Class.prototype, false).chains; // avoid rewatch
}
return this.prototype;
};
return Class;
}
var CoreObject = makeCtor();
CoreObject.PrototypeMixin = Ember.Mixin.create(
/** @scope Ember.CoreObject */ {
reopen: function() {
Ember.Mixin._apply(this, arguments, true);
return this;
},
isInstance: true,
init: function() {},
isDestroyed: false,
isDestroying: false,
/**
Destroys an object by setting the isDestroyed flag and removing its
metadata, which effectively destroys observers and bindings.
If you try to set a property on a destroyed object, an exception will be
raised.
Note that destruction is scheduled for the end of the run loop and does not
happen immediately.
@returns {Ember.Object} receiver
*/
destroy: function() {
if (this.isDestroying) { return; }
this.isDestroying = true;
if (this.willDestroy) { this.willDestroy(); }
set(this, 'isDestroyed', true);
Ember.run.schedule('destroy', this, this._scheduledDestroy);
return this;
},
/**
Invoked by the run loop to actually destroy the object. This is
scheduled for execution by the `destroy` method.
@private
*/
_scheduledDestroy: function() {
Ember.destroy(this);
if (this.didDestroy) { this.didDestroy(); }
},
bind: function(to, from) {
if (!(from instanceof Ember.Binding)) { from = Ember.Binding.from(from); }
from.to(to).connect(this);
return from;
},
toString: function() {
return '<'+this.constructor.toString()+':'+Ember.guidFor(this)+'>';
}
});
CoreObject.__super__ = null;
var ClassMixin = Ember.Mixin.create({
ClassMixin: Ember.required(),
PrototypeMixin: Ember.required(),
isClass: true,
isMethod: false,
extend: function() {
var Class = makeCtor(), proto;
Class.ClassMixin = Ember.Mixin.create(this.ClassMixin);
Class.PrototypeMixin = Ember.Mixin.create(this.PrototypeMixin);
Class.ClassMixin.ownerConstructor = Class;
Class.PrototypeMixin.ownerConstructor = Class;
var PrototypeMixin = Class.PrototypeMixin;
PrototypeMixin.reopen.apply(PrototypeMixin, arguments);
Class.superclass = this;
Class.__super__ = this.prototype;
proto = Class.prototype = o_create(this.prototype);
proto.constructor = Class;
Ember.generateGuid(proto, 'ember');
meta(proto).proto = proto; // this will disable observers on prototype
Class.subclasses = Ember.Set ? new Ember.Set() : null;
if (this.subclasses) { this.subclasses.add(Class); }
Class.ClassMixin.apply(Class);
return Class;
},
create: function() {
var C = this;
if (arguments.length>0) { this._initMixins(arguments); }
return new C();
},
reopen: function() {
this.willReopen();
var PrototypeMixin = this.PrototypeMixin;
PrototypeMixin.reopen.apply(PrototypeMixin, arguments);
return this;
},
reopenClass: function() {
var ClassMixin = this.ClassMixin;
ClassMixin.reopen.apply(ClassMixin, arguments);
Ember.Mixin._apply(this, arguments, false);
return this;
},
detect: function(obj) {
if ('function' !== typeof obj) { return false; }
while(obj) {
if (obj===this) { return true; }
obj = obj.superclass;
}
return false;
},
detectInstance: function(obj) {
return obj instanceof this;
},
/**
In some cases, you may want to annotate computed properties with additional
metadata about how they function or what values they operate on. For example,
computed property functions may close over variables that are then no longer
available for introspection.
You can pass a hash of these values to a computed property like this:
person: function() {
var personId = this.get('personId');
return App.Person.create({ id: personId });
}.property().meta({ type: App.Person })
Once you've done this, you can retrieve the values saved to the computed
property from your class like this:
MyClass.metaForProperty('person');
This will return the original hash that was passed to `meta()`.
*/
metaForProperty: function(key) {
var desc = meta(this.proto(), false).descs[key];
ember_assert("metaForProperty() could not find a computed property with key '"+key+"'.", !!desc && desc instanceof Ember.ComputedProperty);
return desc._meta || {};
},
/**
Iterate over each computed property for the class, passing its name
and any associated metadata (see `metaForProperty`) to the callback.
*/
eachComputedProperty: function(callback, binding) {
var proto = this.proto(),
descs = meta(proto).descs,
empty = {},
property;
for (var name in descs) {
property = descs[name];
if (property instanceof Ember.ComputedProperty) {
callback.call(binding || this, name, property._meta || empty);
}
}
}
});
CoreObject.ClassMixin = ClassMixin;
ClassMixin.apply(CoreObject);
/**
@class
*/
Ember.CoreObject = CoreObject;