Hi!
I've got a list of objects (an observable array) and I would like to click on an object and get a more detailed version of that object, kind of like the single page application tutorial where there is a list of e-mails and clicking one clears the list and shows the email. The problem is that the api-call (via $.get()) returns undefined. I've tried the api in Postman and I get the expeted data there, but when I click an item I get nothing. The rest of the function triggers fine (the array is cleared and the console logs are printed in the console and I get no errors), but I'm not receiving any data.
Knockout.js:
//Data
self.itemList = ko.observable();
self.itemId = ko.observable();
self.chosenItemData = ko.observable();
//Behaviours
self.GetAllItems = function () { //Search function, hence the params. It works just fine.
var params = $('#params').val();
var url = '/api......./' + params;
$.get(url, self.itemList);
}; //GetAllItems
self.goToItem = function (item) { //This one is the issue. Why isn't it returning the expected data? It is triggered by a click event.
self.itemId(item.Id);
self.itemList(null);
var url = 'api/.....' + item.Id+ '/';
console.log(url);
$.get(url, self.chosenItemData);
};
HTML:
<div data-bind="with: chosenItemData">
<img class="card-img--size" data-bind="attr: { src: $data.ImageUrl }" onerror="onItemImageError(this)" />
<strong class="margin-left--m"><span data-bind="text: $data.Name"></span></strong>
</div>
Very grateful for any ideas on why it isn't working and ways to solve it.
//Helene