EmberMonitorable
Ember Mixin for monitoring changes to objects.
Install
Install via npm
or simply download this repository and include ember-monitorable.js
in your project.
Usage
The Ember.Monitorable mixin adds the property hasChanges
to an object.
Extend any object with the Ember.Monitorable mixin and include the property monitoredObservables
:
var Book = Ember.Object.extend(Ember.Monitorable, {
monitoredObservables: ['title']
})
Now, when the Book object's title property changes, Book.get('hasChanges')
will be true
, and false
when those changes are undone.
It is also possible to monitor properties of each element in an array:
var Book = Ember.Object.extend(Ember.Monitorable, {
monitoredObservables: ['publishers.@each.name']
})
In fact, anything which can be observed using Ember's addObserver
can be monitored using the monitoredObservables
property.
Relationships
If another object has the Monitorable mixin included, then its possible to monitor it, or an array of such objects, as a relationship through the monitoredRelationships
property:
var Author = Ember.Object.extend(Ember.Monitorable, {
monitoredObservables: ['name']
})
var Publisher = Ember.Object.extend(Ember.Monitorable, {
monitoredObservables: ['name']
})
var Book = Ember.Object.extend(Ember.Monitorable, {
monitoredObservables: ['title'],
monitoredRelationships: ['author', 'publishers']
})
Identifiers
Sometimes its necessary to differentiate between objects in a relationship by an identifier instead of the objects themselves. To do this, include the option id
with the property used as the identifier:
var Book = Ember.Object.extend(Ember.Monitorable, {
monitoredObservables: ['title'],
monitoredRelationships: ['author', { publishers: { id: '_id' } }]
})
Reset monitoring
To reset monitoring to include all current changes, call Book.commit()
.