Hi
I am trying to build a library that will allow me to make ajax calls to REST services and update my viewmodel with the response. The function looks like this:
function updateObservableArray(observableArray, restResourceUri)
{
// Validate type of observableArray
if (typeof(observableArray) != 'ko.observableArray')
{
alert('Parameter is not an observableArray!');
}
$.ajax({
url: restResourceUri,
dataType: 'json',
success: function(response) {
// response is an array of JSON objects, e.g. [{PersonId: 1, Name: "Bill"}, {PersonId: 2, Name: "John"}]
observableArray(response);
}
});
}
I then call this function as follows:
updateObservableArray(myViewModel.persons, '/webapi/countries');
I would like to validate that the variable passed into this function for the observableArray parameter is actually an observableArray. Of course the above code won't do it since
typeof(someObservableObject)
will always return 'function'. So how do I do this?
Naturally my question also applies to the other KO types:
- ko.subscribable
- ko.observable
- ko.observableArray
- ko.computed