Search code examples
knockout.jsprototypejsknockout-mvcknockout-subscribe

What is the term prototype and subscribe in knockout?


I am going over someone else's code and trying to understand what they have done. There are terms in there that I have no idea what they are.. Prototype, subscribe?? Can someone help me in understanding what this function is doing?

Thanks

myspace.prototype.attached = function () {
    var that = this;
    this.appViewModel.dataLoaded.subscribe(function (val) {
        window.setTimeout(function() {
            that.showUI(val);
        }, 300);
    }, this);
};

Solution

  • All JavaScript objects inherit properties and methods from a prototype. You can find more info here: https://www.w3schools.com/js/js_object_prototypes.asp

    And for subscribe. Please read the documentation of knockout here: http://knockoutjs.com/documentation/observables.html

    In simple words, subscribe is quite similar with computed function, except it will only listen to changes in only 1 observable while computed function will listen to changes in every observables inside it. In your example: If the dataLoaded change, the part inside it will be called