Hey everyone!
I wanted to share some code I've been working on:
$.ajax({
url: '@Url.Action("GetMerchantUsers", "Merchant")',
dataType: 'json',
success: function (json) {
var mappedTasks = $.map(JSON.parse(json), function (item) { return new Task(item) });
self.tasks(mappedTasks);
}
});
This code is calling an MVC controller that returns a list of objects from a JsonResult method, and it's working perfectly fine. However, I need to make some adjustments because now the server will only ever return one Task object instead of multiple. The issue is that when only one task is returned, the .NET JsonResult method doesn't include '[' and ']' at the start and end of the json response. As a result, the $.map() function interprets the properties of the object as a collection rather than just mapping one object to a task observable like I want. Since I'm new to knockout, I'm not sure how to handle this situation - any advice would be greatly appreciated! Let me know if you need more details.
In addition, although I have already mapped the object to a generic JavaScript type, I specifically want to map it to my custom Task type.