6

In my model I have a property that is loaded asynchronously. I want it to generate another model property after it loads.

I was thinking about subscription that could fire after the 1st property changes, generates the 2nd property and then get disposed - I don't know how can I dispose subscription from inside itself.

Is there a way to fire one time event after observable property changes?

0

4 Answers 4

26

To dispose subscription from inside itself just create reference to it:

var subscription = yourObservable.subscribe(function (newValue) {
    ...
    subsription.dispose();
});

If you don't want to write this code every time, you can extend observable with new method:

ko.subscribable.fn.subscribeOnce = function (handler, owner, eventName) {
    var subscription = this.subscribe(function (newValue) {
        subscription.dispose();
        handler(newValue);
    }, owner, eventName);
    return subscription;
};

...

// usage
var subscription = yourObservable.subsribeOnce(yourHandler);

And dont' forget to dispose subscription on component disposal in case it was not fired.

0
0

You could write an extender to suppress all but the first firing:

ko.extenders.fireOnce= function(target) {
    var fired=false;
    var result = ko.computed({
        read: target,
        write: function(newValue) {
            var current = target();
            if (newValue!== current && !fired) {
                fired=true;
                target(newValue);
            }
        }
    });
    result(target());
    return result;
};

then use it as follows:

var myObservable = ko.Observable(blah).extend({fireOnce:null});
3
  • This extender should be named changeOnce, because all value changes except first will be ignored.
    – blazkovicz
    Oct 8, 2014 at 8:59
  • @blazkovicz ...and that's your basis for a downvote?
    – spender
    Oct 8, 2014 at 17:32
  • 2
    yes, because question was about subscription, thast fires once, and you suggested an observable that changes once. These are two hugely different things.
    – blazkovicz
    Oct 10, 2014 at 4:26
0

We ended up with a solution like this ...

const subscription = observable.subscribe(() => {
  subscription.dispose();
  doStuff(settings);
});

Hope somebody can use it.

0

Elaborating on @blazkovicz answer, subscribeOnce could return a Promise eventually resolved with the new value. Open to suggestions on how to expose the subscription object ;-)

ko.subscribable.fn.subscribeOnce = function (handler, owner, eventName) {
    var subscribable = this;
    var subscription;
    return new Promise(function(resolve) {
      subscription = subscribable.subscribe(resolve, owner, eventName);
    }).then(function(newValue) {
      subscription.dispose();
      handler && handler(newValue);
      return newValue;
    });
};

// usage
yourObservable.subscribeOnce().then(function(newValue) {
    ...
});

Your Answer

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.