function sendAjaxRequest(url, type, data, successCallbackFunction, errorCallbackFunction) {
$.ajax({
url: url,
type: type,
data: JSON.stringify(data),
datatype: "json",
processData: false,
contentType: "application/json; charset=utf-8",
async: false,
success: successCallbackFunction,
error: errorCallbackFunction
});
}
function ServiceOverviewViewModel() {
this.testObservable = ko.observable();
this.serviceList = ko.observableArray([]);
this.selectedService = ko.observable();
this.testFunction = function() {
console.log("This is a test function");
};
this.getServiceList = function() {
// Following is a Test URL. In my application, the URL I use returns me list of objects.
sendAjaxRequest("https://httpbin.org/get", "get", null, this.getServiceListSuccess, this.getServiceListError);
};
this.getServiceListSuccess = function(response) {
// In my application I load the serviceList with the list received in the response.
// In this example I am trying to push dummy data.
// This line gives me error as 'this.serviceList is not a function'
this.serviceList({serviceId : 1, serviceName : 'Test'});
};
this.getServiceListError = function(response) {
};
// On Load send the ajax call
this.getServiceList();
}
var serviceOverviewViewModel = new ServiceOverviewViewModel();
ko.applyBindings(serviceOverviewViewModel);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.