Class Ember.CoreObject
create (arguments)
Defined in packages/ember-runtime/lib/system/core_object.js:435
- arguments
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
or use the createWithMixins
shorthand.
createWithMixins (arguments)
Defined in packages/ember-runtime/lib/system/core_object.js:421
- arguments
Equivalent to doing extend(arguments).create()
.
If possible use the normal create
method instead.
destroy Ember.Object
Defined in packages/ember-runtime/lib/system/core_object.js:289
- returns
- Ember.Object
- receiver
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.
eachComputedProperty (callback, binding)
Defined in packages/ember-runtime/lib/system/core_object.js:590
- callback
- Function
- binding
- Object
Iterate over each computed property for the class, passing its name
and any associated metadata (see metaForProperty
) to the callback.
init
Defined in packages/ember-runtime/lib/system/core_object.js:171
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 12 |
App.Person = Ember.Object.extend({ init: function() { this._super(); 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)
Defined in packages/ember-runtime/lib/system/core_object.js:556
- key
- String
- property name
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()
.
reopen
Defined in packages/ember-runtime/lib/system/core_object.js:478
Augments a constructor's prototype with additional properties and functions:
```javascript MyObject = Ember.Object.extend({ name: 'an object' });
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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
Defined in packages/ember-runtime/lib/system/core_object.js:514
Augments a constructor's own properties and functions:
1 2 3 4 5 6 7 8 9 10 11 |
MyObject = Ember.Object.extend({ name: 'an object' }); MyObject.reopenClass({ canBuild: false }); MyObject.canBuild; // false o = MyObject.create(); |
To add functions and properties to instances of
a constructor by extending the constructor's prototype
see reopen
toString String
Defined in packages/ember-runtime/lib/system/core_object.js:338
- returns
- String
- string representation
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>" |
willDestroy
Defined in packages/ember-runtime/lib/system/core_object.js:311
Override to implement teardown.