0

I have my module, as defined here:

import ko = require("knockout");
import http = require("services/http");

class ProductSearch {
    searchText = ko.observable<string>();
    products = ko.observableArray();

    submitSearch = () => {
        http.get(`api/playground/customer/6B4F8534-1580-4973-A379-E2F26DF08D26/products`, { q: this.searchText() })
            .done(this.displayResults);
    };

    displayResults = (results) => {
        ko.mapping.fromJS(results, this.products);
    };
}

export = ProductSearch;

However, when it runs, the ko.mapping.fromJS() call does not appear to be updating the products array at all. I didn't want to clear it and add the items in by hand (which I have confirmed works), as I was led to believe that ko.mapping could manage all that for me, including attempting to maintain the order of items in the array.

Is there something I'm doing wrong?

Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
  • Yep just needed the extra parameter – Jason Spake Apr 25 '17 at 22:44
  • It would be interesting to know *why* it didn't work, since the "overloads" of `fromJS` suggest omitting it should be the same as including the empty options object. – Neil Barnwell Apr 26 '17 at 07:16
  • Where are you seeing an overload signature that suggests it's anything other than the options object? If you only include two arguments it will assume that the second one is the options not the target. So an empty object just serves to put your target observable in the proper third argument slot – Jason Spake Apr 26 '17 at 13:52
  • In the Visual Studio intellisense, as provided by https://www.nuget.org/packages/knockout.mapping.TypeScript.DefinitelyTyped/. – Neil Barnwell Apr 27 '17 at 13:04

0 Answers0