I am working with knockout and have run in to a problem I dont know how to fix. I retrieve a json object back from an asp.net mvc controller. I pass it to the following function:
load = function (data) {
var myViewModel = function () {
var self = this;
ko.mapping.fromJS(data, self);
self.hasItems = ko.computed(function () {
return self.NumberOfItems > 0;
}, self);
};
ko.applyBindings(myViewModel, window.document.getElementById("my-container"));
}
I am adding custom behaviour to my model object based upon what is returned in the json, in particular the NumberOfItems property of the json. My markup is:
<div class="content" style="display: none;" data-bind="visible: hasItems === false">
<span class="empty">My Items</span>
</div>
But I keep getting this error:
Error: Unable to parse bindings. Message: ReferenceError: hasItems is not defined; Bindings value: visible: hasItems === false
I have no idea why - I am a newbie to this so really appreciate any help?
applyBindings expects an object. Try
ko.applyBindings(new myViewModel(), window.document.getElementById("my-container"));
Also, when you want to get the value of an observable, you'll need to use parentheses.
return self.NumberOfItems() > 0;