Skip to content

Commit

Permalink
Merge pull request #177 from martoche/1-4-stable
Browse files Browse the repository at this point in the history
Make .observes() work for yet to be instantiated objects
  • Loading branch information
wagenet committed May 20, 2011
2 parents d4a7d30 + 2d63972 commit d5cd51e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions frameworks/runtime/private/observer_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) ;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
});

0 comments on commit d5cd51e

Please sign in to comment.