Skip to content Skip to sidebar Skip to footer

Observablearray Is Not Defined

I am working in a client development environment and have to adhere to their coding standards. I have the following JS and HTML. My observableArray is coming as not defined. I am n

Solution 1:

You don't expose anotherObservableArray outside the function scope you declare it in. Basically your code is of this format:

{
  onLoad: function (widget) {
    widget.getDetails = function (prod) {
      var anotherObservableArray = ko.observableArray();
      // push some items into the arrayconsole.log(anotherObservableArray());
    };
  }
}

You somehow need to expose the anotherObservableArray outside the function. For example:

{
  onLoad: function (widget) {
    widget.getDetails = function (prod) {
      var anotherObservableArray = ko.observableArray();
      // push some items into the arrayconsole.log(anotherObservableArray());
      this.anotherObservableArray = anotherObservableArray; // Expose it on the function
    };
  }
}

Solution 2:

Move var anotherObservableArray = ko.observableArray(); to your VM definition and ensure it's exposed (i.e. "public"). I am imagining you do have something like this:

var vm = {
    // ...// most likely you are exposing getDetails() already // .... 

    anotherObservableArray: ko.observableArray()
};

// ...

ko.applyBindings(vm);

Post a Comment for "Observablearray Is Not Defined"