I'm retuning a JSON object from a web service and using knockout 2.01 to display the results. I'm able to display my results without an issue but as soon as I try to add a computed observable into the view model I get the error below.
What am I missing with this code below?
Uncaught TypeError: Object # has no method 'dateCreated'
!--- JSON
{"jobTitle":"Digital designer","dateCreated":"7/7/2012","timeCreated":"2:27 PM","location":"Perth","jobSummary":"one\ntwo\nthree","Uri":"/candidates/view-all-jobs/digital-designer/","CreateDate":"\/Date(1341635236000)\/"}
!-- html
<div data-bind="foreach: $data">
<article>
<header>
<time datetime="2007-08-29T13:58Z"></time>
<h3><a target="_self" data-bind="text: $data.location"> </a> </h3>
</header>
<span data-bind="text: $data.fullDate"> </span> </article>
</div>
!-- code
var viewModel = ko.observableArray();
$(document).ready(function () {
if ($('#featured-jobs').length > 0) {
$.ajax({
type: 'POST',
url: "handler/jobServer.ashx",
data: { 'jobType': '1' },
success: OnComplete,
error: OnFail,
dataType: 'json'
});
}
});
function OnComplete(result) {
var self = this;
ko.mapping.fromJS(result, {}, viewModel);
self.fullDate = ko.computed(
function () {
return self.dateCreated() + " " + self.timeCreated();
}, self);
ko.applyBindings(new viewModel());
}
function OnFail(result) {
alert('Request Failed');
}