Ember.CoreObject Class packages/ember-runtime/lib/system/core_object.js:221
PUBLIC
Defined in: packages/ember-runtime/lib/system/core_object.js:221
Module: ember-runtime
Methods
- _lazyInjections
- _onLookup
- _scheduledDestroy
- create
- destroy
- eachComputedProperty
- extend
- init
- metaForProperty
- reopen
- reopenClass
- toString
- willDestroy
Properties
_lazyInjections
Object
private
Returns a hash of property names and container names that injected properties will lookup on the container lazily.
Returns:
- Object
- Hash of all lazy injected property keys to container names
_onLookup
private
Provides lookup-time type validation for injected properties.
_scheduledDestroy
private
Invoked by the run loop to actually destroy the object. This is
scheduled for execution by the destroy
method.
create
(arguments)
public
static
Creates an instance of a class. Accepts either no arguments, or an object containing values to initialize the newly instantiated object with.
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 |
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
.
Parameters:
- arguments []
destroy
Ember.Object
public
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:
- Ember.Object
- receiver
eachComputedProperty
(callback, binding)
private
static
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)
public
static
Creates a new subclass.
1 2 3 4 5 |
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:
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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]
- One or more Mixin classes
- arguments [Object]
- Object containing values to use within the new class
init
public
An overridable method called when objects are instantiated. By default, does nothing unless it is overridden during class definition.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
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
,
be sure to call this._super(...arguments)
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)
private
static
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:
1 2 3 4 |
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:
1 |
MyClass.metaForProperty('person');
|
This will return the original hash that was passed to meta()
.
Parameters:
- key String
- property name
reopen
public
Augments a constructor's prototype with additional properties and functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
public
Augments a constructor's own properties and functions:
1 2 3 4 5 6 7 8 9 10 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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
public
Returns a string representation which attempts to provide more information
than Javascript's toString
typically does, in a generic way for all Ember
objects.
1 2 3 |
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:
1 2 3 |
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.
1 2 3 4 5 6 7 |
App.Teacher = App.Person.extend({ toStringExtension: function() { return this.get('fullName'); } }); teacher = App.Teacher.create() teacher.toString(); //=> "<App.Teacher:ember1026:Tom Dale>" |
Returns:
- String
- string representation
willDestroy
public
Override to implement teardown.
concatenatedProperties
Array
public
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 6 |
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:
1 2 3 4 |
var view = App.FooBarView.create({ classNames: 'baz' }) view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz'] |
Using the concatenatedProperties
property, we can tell Ember to 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
public
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
public
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
mergedProperties
Array
public
Defines the properties that will be merged 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 merging the superclass property value
with the subclass property's value. An example of this in use within Ember
is the queryParams
property of routes.
Here is some sample code showing the difference between a merged property and a normal one:
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 |
App.BarRoute = Ember.Route.extend({ someNonMergedProperty: { nonMerged: 'superclass value of nonMerged' }, queryParams: { page: {replace: false}, limit: {replace: true} } }); App.FooBarRoute = App.BarRoute.extend({ someNonMergedProperty: { completelyNonMerged: 'subclass value of nonMerged' }, queryParams: { limit: {replace: false} } }); var fooBarRoute = App.FooBarRoute.create(); fooBarRoute.get('someNonMergedProperty'); // => { completelyNonMerged: 'subclass value of nonMerged' } // // Note the entire object, including the nonMerged property of // the superclass object, has been replaced fooBarRoute.get('queryParams'); // => { // page: {replace: false}, // limit: {replace: false} // } // // Note the page remains from the superclass, and the // `limit` property's value of `false` has been merged from // the subclass. |
This behavior is not available during object create
calls. It is only
available at extend
time.
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 merged property (to not mislead your users to think they can override the property in a subclass).
Default: null