Testing if a JavaScript variable is a KO observable

3,388 views
Skip to first unread message

loei

unread,
Jul 26, 2012, 9:52:40 AM7/26/12
to knock...@googlegroups.com
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

loei

unread,
Jul 26, 2012, 2:25:33 PM7/26/12
to knock...@googlegroups.com
I've found the following functions that can be used for this:

  • ko.isComputed(myVar)
    • returns true if myVar is a computed()
  • ko.isObservable(myVar)
    • returns true if myVar is an observable()
    • returns true if myVar is an observableArray()
I think that an isSubscribable function will be less useful as I don't think you can instantiate it. 

I could not find a way to distinguish between an observable and an observableArray though. If anyone can show me how to do that it would be greatly appreciated.

Michael "Kato" Wulf

unread,
Jul 26, 2012, 2:40:38 PM7/26/12
to knock...@googlegroups.com
Have you tried if( ko.isObservable(myVar) && myVar.push )?
--
Michael Wulf


loei

unread,
Jul 26, 2012, 3:21:04 PM7/26/12
to knock...@googlegroups.com
I tried that:

            var a = ko.observableArray();
            alert(ko.isObservable(a) && a.push != undefined);

At first it seems to work, but unfortunately that is not reliable. I can create an observable() and then add my own function called 'push' to it causing this test to fail:

            var a = ko.observable();
            a.push = function myOwnFunction() { };
            alert(ko.isObservable(a) && a.push != undefined);

I would like to use a KO built-in function that will determine whether something is an observable or an observableArray definitively/reliably if possible.

But thank you; this is a sufficient work-around for now.

Michael "Kato" Wulf

unread,
Jul 26, 2012, 3:26:40 PM7/26/12
to knock...@googlegroups.com
Sure. You could also override ko.observableArray; this is JavaScript after all : )

But it would of course be a bad idea (just like overriding push--or most anything--on a knockout observable).

Something built in would be nice, but it's okay to accept the loosely scripted model too. Feature detection is an acceptable, if more annoying, alternative.

Make sure you're not overly complicating your job by being a perfectionist (a flaw I invoke far too often myself!)

Michael "Kato" Wulf

unread,
Jul 26, 2012, 3:31:20 PM7/26/12
to knock...@googlegroups.com
I wonder what ko.hasPrototype(var, ko.observableArray); says?

loei

unread,
Jul 26, 2012, 5:08:34 PM7/26/12
to knock...@googlegroups.com
I've tried out isPrototypeOf (I cannot find the hasPrototype function), but I cannot figure out what it does and the documentation doesn't seem to include any reference to it.

It's not that I'm trying to be a perfectionist; it's more that I'm trying to build a framework for other developers to use at my company. That means that I have to cater for scenarios where someone will try to use my framework incorrectly and have it fail gracefully or display a clear and concise error. On the other hand, I come from a C# background and only recently started expanding my JavaScript knowledge. So maybe you're right; maybe I'm just being too pedantic.

Thanks for the help :)

rpn

unread,
Jul 26, 2012, 9:59:15 PM7/26/12
to knock...@googlegroups.com
observableArrays are really just observables that have had extra functions added to them to do array operations while triggering notifications.  Your best bet is to check if it is an observable and then check for one or more of the array methods.
Reply all
Reply to author
Forward
0 new messages