0
我正在從Web服務中重新調用JSON對象,並使用knockout 2.01來顯示結果。我能夠顯示我的結果沒有問題,但只要我嘗試添加一個計算的觀察到視圖模型我得到下面的錯誤。knockoutjs - Computed Observable返回錯誤
我在下面的代碼中遺漏了什麼?
遺漏的類型錯誤:對象#有沒有方法 '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>
- 代碼
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');
}
!!
是的,正好對 – 2012-07-16 16:39:07
好的。有道理。我怎麼寫這個? var viewModel = ko.observableArray(); – user686483 2012-07-16 22:13:59
好的。有道理。 @Arbiter我怎麼寫這個?我會將它添加到viewModel的聲明中嗎?我能用語法得到一些幫助嗎? var viewModel = ko.observableArray(); – user686483 2012-07-16 22:23:02