我有以下代码和数据
{
"Job": {
"JobName": null,
"JobNumber": 13,
"JobSize": 0,
"Status": "Finished",
"DateCreated": "/Date(1312551946280)/"
},
"Values": [
{
"Id": "8401",
"Status": "Good",
"JobNumber": 13
},
{
"Id": "8402",
"Status": "Bad",
"JobNumber": 13
}
]
}
function JobViewModel() {
var self = this;
self.jobs = ko.observableArray();
self.selectedJob = ko.observable();
self.history = ko.observableArray();
var app = Sammy(function () {
this.get('#/:jobNumber', function (context) {
$.get('/home/GetJobInfo/' + context.params.jobNumber, {}, self.selectedJob);
});
});
jQuery(function () {
app.run();
});
}
我知道 $.get 正在工作,因为我可以看到返回的 JSON 数据,并且我在 $.get 的第三个参数中使用了一个函数来查看数据。
当我用 self.selectedJob 属性替换该函数时,为什么它没有被填充?
谢谢
更新:这是我的 HTML
<!DOCTYPE html>
<html>
<head>
<link href="~/Content/main.css" rel="stylesheet" />
<title>Sentinel Web Data</title>
</head>
<body>
<div id="sidebar">
<ul data-bind="foreach: jobs">
<li><a data-bind="text: $data.JobName, attr: { href: '#/' + $data.JobNumber}"></a></li>
</ul>
</div>
<div id="job">
<span data-bind="text: JobNumber"></span>
</div>
<div class="clear"></div>
<div id="history" data-bind="with: history"></div>
<script src="~/Scripts/knockout-2.2.1.js"></script>
<script src="~/Scripts/jquery-1.4.4.min.js"></script>
<script src="~/Scripts/sammy-0.7.4.min.js"></script>
<script src="~/Scripts/JobViewModel.js"></script>
<script>
ko.applyBindings(new JobViewModel());
</script>
</body>
</html>
当查看控制台中的 get 并查看 self.selectedJob.Job 时,我得到“未定义”