I'm using the ko mapping plugin to create my observables from a JS object, ko.mapping.fromJS()
.
A snippet of the object is below:
{ Name: "blah", Parent: { Title: "blah", Url: "/blah" } }
If Parent.Title
changes then everything on my page is updating as expected, but when Parent
becomes null I have a problem. The simplified markup that uses the Parent
properties looks like this:
<p data-bind="if: HasParent">Up: <a data-bind="text: ParentTitle"></a></p>
HasParent
looks like this:
self.HasParent = ko.computed(function () {
return self.Parent;
});
ParentTitle
looks like this:
self.ParentTitle = ko.computed(function () {
return self.HasParent() ? self.Parent.Title() : "";
});
Note: self
is set to the result back from ko.mapping.fromJS()
and then applied to the page by calling ko.applyBindings();
So basically my problem is the HasParent
function is always returning a true-ish value.
Also, this is my first ko project so if I can do anything in a better way please let me know :)
Any help would be appreciated.
Thanks.