Ember.CoreObject Class
Item Index
Methods
- _scheduledDestroy
- create static
- createWithMixins static
- destroy
- eachComputedProperty
- extend static
- init
- metaForProperty
- reopen
- reopenClass
- toString
- willDestroy
Properties
Methods
_scheduledDestroy
()
private
Invoked by the run loop to actually destroy the object. This is
scheduled for execution by the destroy
method.
create
-
[arguments]
Creates an instance of a class. Accepts either no arguments, or an object containing values to initialize the newly instantiated object with.
App.Person = Ember.Object.extend({
helloWorld: function() {
alert("Hi, my name is " + this.get('name'));
}
});
var tom = App.Person.create({
name: 'Tom Dale'
});
tom.helloWorld(); // alerts "Hi, my name is Tom Dale".
create
will call the init
function if defined during
Ember.AnyObject.extend
If no arguments are passed to create
, it will not set values to the new
instance during initialization:
var noName = App.Person.create();
noName.helloWorld(); // alerts undefined
NOTE: For performance reasons, you cannot declare methods or computed
properties during create
. You should instead declare methods and computed
properties when using extend
or use the createWithMixins
shorthand.
Parameters:
-
[arguments]
Object optional multiple
createWithMixins
-
[arguments]
Equivalent to doing extend(arguments).create()
.
If possible use the normal create
method instead.
Parameters:
-
[arguments]
Object optional multiple
destroy
()
Ember.Object
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. It will set an isDestroying flag immediately.
Returns:
receiver
eachComputedProperty
-
callback
-
binding
Iterate over each computed property for the class, passing its name
and any associated metadata (see metaForProperty
) to the callback.
Parameters:
-
callback
Function -
binding
Object
extend
-
[mixins]
-
[arguments]
Creates a new subclass.
App.Person = Ember.Object.extend({
say: function(thing) {
alert(thing);
}
});
This defines a new subclass of Ember.Object: App.Person
. It contains one method: say()
.
You can also create a subclass from any existing class by calling its extend()
method. For example, you might want to create a subclass of Ember's built-in Ember.View
class:
App.PersonView = Ember.View.extend({
tagName: 'li',
classNameBindings: ['isAdministrator']
});
When defining a subclass, you can override methods but still access the implementation of your parent class by calling the special _super()
method:
App.Person = Ember.Object.extend({
say: function(thing) {
var name = this.get('name');
alert(name + ' says: ' + thing);
}
});
App.Soldier = App.Person.extend({
say: function(thing) {
this._super(thing + ", sir!");
},
march: function(numberOfHours) {
alert(this.get('name') + ' marches for ' + numberOfHours + ' hours.')
}
});
var yehuda = App.Soldier.create({
name: "Yehuda Katz"
});
yehuda.say("Yes"); // alerts "Yehuda Katz says: Yes, sir!"
The create()
on line #17 creates an instance of the App.Soldier
class. The extend()
on line #8 creates a subclass of App.Person
. Any instance of the App.Person
class will not have the march()
method.
You can also pass Mixin
classes to add additional properties to the subclass.
App.Person = Ember.Object.extend({
say: function(thing) {
alert(this.get('name') + ' says: ' + thing);
}
});
App.SingingMixin = Mixin.create({
sing: function(thing){
alert(this.get('name') + ' sings: la la la ' + thing);
}
});
App.BroadwayStar = App.Person.extend(App.SingingMixin, {
dance: function() {
alert(this.get('name') + ' dances: tap tap tap tap ');
}
});
The App.BroadwayStar
class contains three methods: say()
, sing()
, and dance()
.
Parameters:
-
[mixins]
Mixin optional multipleOne or more Mixin classes
-
[arguments]
Object optional multipleObject containing values to use within the new class
init
()
An overridable method called when objects are instantiated. By default, does nothing unless it is overridden during class definition.
Example:
App.Person = Ember.Object.extend({
init: function() {
alert('Name is ' + this.get('name'));
}
});
var steve = App.Person.create({
name: "Steve"
});
// alerts 'Name is Steve'.
NOTE: If you do override init
for a framework class like Ember.View
or
Ember.ArrayController
, be sure to call this._super()
in your
init
declaration! If you don't, Ember may not have an opportunity to
do important setup work, and you'll see strange behavior in your
application.
metaForProperty
-
key
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()
.
Parameters:
-
key
Stringproperty name
reopen
()
Augments a constructor's prototype with additional properties and functions:
MyObject = Ember.Object.extend({
name: 'an object'
});
o = MyObject.create();
o.get('name'); // 'an object'
MyObject.reopen({
say: function(msg){
console.log(msg);
}
})
o2 = MyObject.create();
o2.say("hello"); // logs "hello"
o.say("goodbye"); // logs "goodbye"
To add functions and properties to the constructor itself,
see reopenClass
reopenClass
()
Augments a constructor's own properties and functions:
MyObject = Ember.Object.extend({
name: 'an object'
});
MyObject.reopenClass({
canBuild: false
});
MyObject.canBuild; // false
o = MyObject.create();
In other words, this creates static properties and functions for the class. These are only available on the class and not on any instance of that class.
App.Person = Ember.Object.extend({
name : "",
sayHello : function(){
alert("Hello. My name is " + this.get('name'));
}
});
App.Person.reopenClass({
species : "Homo sapiens",
createPerson: function(newPersonsName){
return App.Person.create({
name:newPersonsName
});
}
});
var tom = App.Person.create({
name : "Tom Dale"
});
var yehuda = App.Person.createPerson("Yehuda Katz");
tom.sayHello(); // "Hello. My name is Tom Dale"
yehuda.sayHello(); // "Hello. My name is Yehuda Katz"
alert(App.Person.species); // "Homo sapiens"
Note that species
and createPerson
are not valid on the tom
and yehuda
variables. They are only valid on App.Person
.
To add functions and properties to instances of
a constructor by extending the constructor's prototype
see reopen
toString
()
String
Returns a string representation which attempts to provide more information
than Javascript's toString
typically does, in a generic way for all Ember
objects.
App.Person = Em.Object.extend()
person = App.Person.create()
person.toString() //=> "<App.Person:ember1024>"
If the object's class is not defined on an Ember namespace, it will indicate it is a subclass of the registered superclass:
Student = App.Person.extend()
student = Student.create()
student.toString() //=> "<(subclass of App.Person):ember1025>"
If the method toStringExtension
is defined, its return value will be
included in the output.
App.Teacher = App.Person.extend({
toStringExtension: function() {
return this.get('fullName');
}
});
teacher = App.Teacher.create()
teacher.toString(); //=> "<App.Teacher:ember1026:Tom Dale>"
Returns:
string representation
willDestroy
()
Override to implement teardown.
Properties
concatenatedProperties
Array
Defines the properties that will be concatenated from the superclass (instead of overridden).
By default, when you extend an Ember class a property defined in
the subclass overrides a property with the same name that is defined
in the superclass. However, there are some cases where it is preferable
to build up a property's value by combining the superclass' property
value with the subclass' value. An example of this in use within Ember
is the classNames
property of Ember.View
.
Here is some sample code showing the difference between a concatenated property and a normal one:
App.BarView = Ember.View.extend({
someNonConcatenatedProperty: ['bar'],
classNames: ['bar']
});
App.FooBarView = App.BarView.extend({
someNonConcatenatedProperty: ['foo'],
classNames: ['foo'],
});
var fooBarView = App.FooBarView.create();
fooBarView.get('someNonConcatenatedProperty'); // ['foo']
fooBarView.get('classNames'); // ['ember-view', 'bar', 'foo']
This behavior extends to object creation as well. Continuing the above example:
var view = App.FooBarView.create({
someNonConcatenatedProperty: ['baz'],
classNames: ['baz']
})
view.get('someNonConcatenatedProperty'); // ['baz']
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']
Adding a single property that is not an array will just add it in the array:
var view = App.FooBarView.create({
classNames: 'baz'
})
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']
Using the concatenatedProperties
property, we can tell to Ember that mix
the content of the properties.
In Ember.View
the classNameBindings
and attributeBindings
properties
are also concatenated, in addition to classNames
.
This feature is available for you to use throughout the Ember object model, although typical app developers are likely to use it infrequently. Since it changes expectations about behavior of properties, you should properly document its usage in each individual concatenated property (to not mislead your users to think they can override the property in a subclass).
Default: null
isDestroyed
Unknown
Destroyed object property flag.
if this property is true
the observers and bindings were already
removed by the effect of calling the destroy()
method.
Default: false
isDestroying
Unknown
Destruction scheduled flag. The destroy()
method has been called.
The object stays intact until the end of the run loop at which point
the isDestroyed
flag is set.
Default: false