我无法加载我创建的json对象

时间:2014-03-18 00:18:07

标签: json knockout.js

我正在使用KnockoutJS作为网页。我创建了一个JSON对象来模仿来自服务器的值。每次我运行页面时,我都会得到一个空记录。我错过了什么吗?

var data ={ "employee": [{
"SiteId": 9,
    "SystemId": 1,
    "SystemName": "Glocus",
    "ManualLink": "http://www.Glocus.com/Guide",
    "ContactNo": "301-489-8989",
    "UserAccount": [{
    "AccountId": 1,
        "AccountName": "Sisij",
        "AccountType": "Customer System",
        "Username": "Tybeke"
        "Password": "*****",
}, {
    "AccountId": 2,
         "AccountName": "gabid",
        "AccountType": "System System",
        "Username": "Tybeke"
        "Password": "*****",
}]
}, {
"SiteId": "1",
   "SystemId": 1,
    "SystemName": "TheSecondWorld",
    "ManualLink": "http://www.TheSecondWorld.com/Guideplan",
    "ContactNo": "301-489-8989",
    "UserAccount": [{
    "AccountId": 1,
        "AccountName": "Uniqex",
        "AccountType": "Customer System",
        "Username": "TUniqux"
        "Password": "*****",
}, {
    "AccountId": 2,
         "AccountName": "Celind",
        "AccountType": "System System",
        "Username": "GCElinds"
        "Password": "*****",
}]
}]};


var ViewModel = function () {
var self = this;
var hello = ko.mapping.fromJS(data);
self.items = ko.observableArray(hello);
};

ko.applyBindings(new ViewModel());

这是我的knockoutJS Html代码:

<div>
 <span>Account</span>
<hr/>
<table>
    <thead>
        <tr>
            <th>System Name</th>
            <th>Manual link</th>
            <th>Contact No</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: items.employee">
        <tr>
            <td data-bind="text :SystemName"></td>
            <td data-bind="text : ManualLink"></td>
            <td data-bind="text : ContactNo "></td>
        </tr>
    </tbody>
</table>
<hr/>
</div>

我浪费了太多时间试图把它弄好。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

ko.mapping is used to create viewmodels。因此,在这种情况下,您实际上不会嵌套视图模型;你只需使用JSON创建一个:

viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);

http://jsfiddle.net/32765/1/