10

I have a ArrayController for date picker with properties to and from. Now when user selects a new date range from the UI both to and from value changes.

So a function which observers on to and from is trigger twice for a single change in date range.

I want to trigger the function only one every date range change. Any suggestions how to do that with ember

    app = Ember.Application.create();

    app.Time = Ember.ArrayController.create({

        to: null,
        from: null,
        rootElement: "#time",

        debug1: function() {
            this.set('to', 1);
            this.set('from', 2);
        },

        debug2: function() {
            this.setProperties( {
                'to': 3,
                'from': 4
            });
        },

        debug3: function() {
            this.beginPropertyChanges();
            this.set('to', 5);
            this.set('from', 6);
            this.endPropertyChanges();
        },

        search: function() {
            alert('called');
        }.observes('to', 'from')
    });

View in JS Fiddle

2
  • what about observing only to or from ? as the two are changing when the user select a nex date, relying on only one of two could work no ?
    – sly7_7
    Nov 9, 2012 at 22:21
  • well there are also other ways to change the to and from also. So the observer has to be on both Nov 9, 2012 at 22:42

2 Answers 2

12

Perhaps you can introduce a computed property on from and to, and then plug the observer on this property. As CPs are cached by default, I think the observer will be triggered only once.

date: function(){
 // return the computed date
}.property('from', 'to')

dateDidChange: function(){

}.observes('date')

EDIT Thanks to your fiddle, it seems to work using setProperties, or begin/end propertyChanges http://jsfiddle.net/Sly7/zf2q3/1/

4

You can wrap observer in a Em.run.once

_dateDidChange: function(){
  Ember.run.once(this, 'dateDidChange');
}.observes('from', 'to'),

dateDidChange: function() {
  // should be called only once
}
3
  • @AnkurAgarwal Hum, I think this is better what I did. In fact, I have asked for Ember.js contributors to have a look at this. That's why he suggested an other workaround.
    – sly7_7
    Nov 15, 2012 at 8:11
  • Ankur, can you extrapolate? We are thinking about make observers async, so you will not have to wrap it, it will juste work.
    – Tchak
    Nov 15, 2012 at 10:01
  • @Tchak If I use this way, it will work. But being the publisher subscriber paradigm, Lets say if anyone outside of this object want to observes the changes them it has to explicitly declared in here. This is a nightmare because sometime external observers exist and sometime they dont. In first answer by sly7_7, external observers can easily observe without them being explicitly declared and no extra code or handling. Nov 15, 2012 at 22:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.