#90 Observers Run Once

If you have an observer like this:

Then in your code, you write:

Your fullName observer will fire twice because both dependent properties changed.  You can work around this by using Ember.run.once:

This will ensure that any processing you need to do only happens once, and happens in the next run loop once all bindings are synchronized.

Published by

Emad Ibrahim

Author of asp .net MVC & TDD, entrepreneur, passionate technologist, programmer and productivity nut. more at http://t.co/oSJXPvzu

  • Tschoartschi

    I always try to avoid observers. Right now I can not think about a case were you have to use observers. I was able to refactor all my observers with the “life cycle hooks” which components brought to Ember.

    • I am always nervous using observers but I can’t imagine completely getting rid of them. How would you handle this situation without observers?

      “you have a list of products in a shopping cart, when the user changes the quantity, you want to display a message if the quantity is > 5”

      Usually I would observe cart.items.[].quantity and then fire some alert(“Quantity is too high”) or something along these lines.

      PS: I know this is a bad example, but just go with it 🙂