Ember.Freezable Class packages/ember-runtime/lib/mixins/freezable.js:9
Extends: Ember.Mixin
Defined in: packages/ember-runtime/lib/mixins/freezable.js:9
Module: ember-runtime
The Ember.Freezable mixin implements some basic methods for marking an
object as frozen. Once an object is frozen it should be read only. No changes
may be made the internal state of the object.
Enforcement
To fully support freezing in your subclass, you must include this mixin and
override any method that might alter any property on the object to instead
raise an exception. You can check the state of an object by checking the
isFrozen property.
Although future versions of JavaScript may support language-level freezing
object objects, that is not the case today. Even if an object is freezable,
it is still technically possible to modify the object, even though it could
break other parts of your application that do not expect a frozen object to
change. It is, therefore, very important that you always respect the
isFrozen property on all freezable objects.
Example Usage
The example below shows a simple object that implement the Ember.Freezable
protocol.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Contact = Ember.Object.extend(Ember.Freezable, {
firstName: null,
lastName: null,
// swaps the names
swapNames: function() {
if (this.get('isFrozen')) throw Ember.FROZEN_ERROR;
var tmp = this.get('firstName');
this.set('firstName', this.get('lastName'));
this.set('lastName', tmp);
return this;
}
});
c = Context.create({ firstName: "John", lastName: "Doe" });
c.swapNames(); // returns c
c.freeze();
c.swapNames(); // EXCEPTION
|
Copying
Usually the Ember.Freezable protocol is implemented in cooperation with the
Ember.Copyable protocol, which defines a frozenCopy() method that will
return a frozen object, if the object implements this method as well.
apply
(obj)
Parameters:
- obj
Returns:
- applied object
create
(arguments)
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
detect
(obj)
Boolean
Parameters:
- obj
Returns:
- Boolean
freeze
Object
Freezes the object. Once this method has been called the object should no longer allow any properties to be edited.
Returns:
- Object
- receiver
reopen
(arguments)
Parameters:
- arguments
isFrozen
Boolean
/**
Set to true when the object is frozen. Use this property to detect
whether your object is frozen or not.
Fork Us!