1

My question is pretty straight forward. I have JS component (an AMD module that extends from uiComponent) and I know I can make a property observable by specifying the tracks object in the defaults object for e.g.

defaults: { myProeperty: "Some Value", tracks: { myProperty: true }}

I wish now to subscribe to that change. For example execute a console.log every time myProperty value changes. How do I do that?

1 Answer 1

2

A Variable will be subscribed when it's type observable ko.observable

Your code will be like:

Component.extend({
     ......
    myProeperty = ko.observable(false),
    ...............
    defaults: {
        myProeperty: "Some Value",
        tracks: {
            myProperty: true
        }
    },
    initialize: function() {
        self = this;
        this._super();
        this.myProeperty.subscribe(function(newValue) {
            console.log(newValue);

        });
    }

}
2
  • Thank you for your answer. Is there another way though? Another object in the defaults configuration? smth like subscribe { myProperty : myFunc } or anything that does not require to extend the initialize method? Commented Dec 8, 2019 at 8:37
  • I think you could use the import node. Commented Dec 8, 2019 at 8:41

Your Answer

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

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