Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #177 from martoche/1-4-stable

Make .observes() work for yet to be instantiated objects
  • Loading branch information...
commit d5cd51eb7886e2a9982bc905205eecbec61dcb08 2 parents d4a7d30 + 2d63972
@wagenet wagenet authored
View
4 frameworks/runtime/private/observer_queue.js
@@ -47,7 +47,7 @@ SC.Observers = {
}
// if a tuple was found, add the observer immediately...
- if (tuple) {
+ if (tuple && !tuple[0].isClass) {
tuple[0].addObserver(tuple[1],target, method) ;
// otherwise, save this in the queue.
@@ -107,7 +107,7 @@ SC.Observers = {
if (!item) continue ;
var tuple = SC.tupleForPropertyPath(item[0], item[3]);
- if (tuple) {
+ if (tuple && !tuple[0].isClass) {
tuple[0].addObserver(tuple[1], item[1], item[2]) ;
} else newQueue.push(item) ;
}
View
41 ...ts/private/observer_queue/isObservingSuspended.js → frameworks/runtime/tests/private/observer_queue.js
@@ -53,3 +53,44 @@ test("suspended notifications should work when nesting property change groups",
SC.Observers.resumePropertyObserving();
equals(callCount, 1, 'should notify observer when resumed');
});
+
+
+module("SC.Observers.addObserver");
+
+test("Object not yet instantiated", function() {
+ var garage, car, observer;
+
+ garage = SC.Object.create({
+ car: SC.Object.extend({
+ make: null
+ })
+ });
+
+ car = garage.get('car');
+
+ observer = SC.Object.create({
+ callCount: 0,
+ makeDidChange: function() {
+ this.callCount += 1;
+ }
+ });
+
+ ok(car.isClass, "The car object is not yet an instance, it's a class for now.");
+
+ // 1. SC.Observers.addObserver should queue the class, waiting for it to be an instance
+ SC.Observers.addObserver('car.make', observer, 'makeDidChange', garage);
+ ok(SC.Observers.queue.some(function(el) { return el[1] === observer; }), "The observer should have been queued because the car object is a class, not an instance.");
+
+ // 2. A call to SC.Observers.flush should leave the class in the queue because it's not yet an instance
+ SC.Observers.flush(garage);
+ ok(SC.Observers.queue.some(function(el) { return el[1] === observer; }), "The observer should still be in the queue.");
+
+ // 3. After we instantiate the class, a call to SC.Observers.flush shoud remove the object from the queue...
+ car = garage.car = car.create({ make: 'Renault' });
+ SC.Observers.flush(garage);
+ ok(!SC.Observers.queue.some(function(el) { return el[1] === observer; }), "The observer should have been removed from the queue.");
+
+ // 4. ...and the observer should work
+ car.set('make', 'Ferrari');
+ equals(observer.callCount, 1, "The observer should have been called once.");
+});
Please sign in to comment.
Something went wrong with that request. Please try again.