J B
J B

Reputation: 400

Ember observer not getting most recent value

I'm trying to observe a controller property in its associated view, but when I do so, I always get the old value. I know about the async issues with observers, as per:

http://emberjs.com/guides/object-model/observers/

but using run.once() doesn't seem to help. Here's my code:

MyController = Ember.Controller.extend({

  testProperty: false,

  otherThingWatcher: function() {

    this.set('testProperty', true);
    console.log(this.get('testProperty')); // Outputs true when someOtherThing changes.

  }.observes('someOtherThing');

});

MyView = Ember.View.Extend({

  checkTestProperty: function() {

    console.log(this.get('controller.testProperty')); // Ack! This is false!

    var that = this;
    Ember.run.once(this, function() {

      console.log(this.get('controller.testProperty')); // Same thing.

    });

  }.observes('controller.testProperty');

});

What have I missed?

Versions:
DEBUG: Ember      : 1.5.1 ember.js:3521
DEBUG: Ember Data : 1.0.0-beta.8+canary.503c75c9 ember.js:3521
DEBUG: Handlebars : 1.3.0 ember.js:3521
DEBUG: jQuery     : 1.9.1 

Upvotes: 1

Views: 266

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

There are a few problems with your code.

  1. You need to use extend, not Extend
  2. your observes can't end in a ;

Fixed Code

MyView = Ember.View.extend({

  checkTestProperty: function() {

    console.log(this.get('controller.testProperty')); // Ack! This is false!

    var that = this;
    Ember.run.once(this, function() {

      console.log(this.get('controller.testProperty')); // Same thing.

    });

  }.observes('controller.testProperty')

});

http://emberjs.jsbin.com/cixoqeqi/1/edit

Upvotes: 1

Related Questions