Permalink
Browse files

Add a two-arg observe method

  • Loading branch information...
1 parent 03f28bc commit dd6c3a69f50658bafcac7b0b3de882a7b25ff4f8 @kriszyp kriszyp committed Feb 13, 2014
Showing with 21 additions and 2 deletions.
  1. +18 −0 Model.js
  2. +1 −0 docs/DataModelling.md
  3. +2 −2 tests/Model.js
View
@@ -301,6 +301,20 @@ define([
return value;
},
+ observe: function (/*string*/ key, /*function*/ listener, /*object*/ options) {
+ // summary:
+ // Registers a listener for any changes in the specified property
+ // key:
+ // The name of the property to listen to
+ // listener:
+ // Function to be called for each change
+ // options.onlyFutureUpdates
+ // If this is true, it won't call the listener for the current value,
+ // just future updates. If this is true, it also won't return
+ // a new reactive object
+ return this.property(key).observe(listener, options);
+ },
+
validate: function (/*string[]?*/ fields) {
// summary:
// Validates the current object.
@@ -384,6 +398,10 @@ define([
// just future updates. If this is true, it also won't return
// a new reactive object
+ if (typeof listener === 'string') {
+ // a property key was provided, use the Model's method
+ return this.inherited(arguments);
+ }
if (!options || !options.onlyFutureUpdates) {
// create a new reactive to contain the results of the execution
// of the provided function
View
@@ -16,6 +16,7 @@ Method | Description
`get(name)` | This returns the property value with the given name.
`set(name, value)` | This sets the value of a property.
`property(name)` | This returns a property object instance for the given name.
+`observe(name, listener, options)` | This will listen for any changes to the value of the given property. See the Property's observe for the options.
`validate()` | This will validate the object, determining if there are any errors on the object.
`save()` | This will save the object, validating and then storing the object in the store.
`remove()` | This will delete the object from the object store.
View
@@ -251,7 +251,7 @@ define([
model.set('firstName', 'Jane');
assert.strictEqual(updatedName, 'Jane Doe');
var updatedName2;
- var handle = model.property('name').observe(function (name) {
+ var handle = model.observe('name', function (name) {
updatedName2 = name;
});
assert.strictEqual(updatedName2, 'Jane Doe');
@@ -270,7 +270,7 @@ define([
model.set('birthDate', then);
assert.strictEqual(model.get('birthDate').getTime(), 1000000);
var updatedDate, now = new Date();
- model.property('birthDate').observe(function (newDate) {
+ model.observe('birthDate', function (newDate) {
updatedDate = newDate;
});
model.set('birthDate', now);

0 comments on commit dd6c3a6

Please sign in to comment.