Kendo-UI数据源为空

时间:2013-11-30 16:44:31

标签: javascript knockout.js kendo-ui

我在kendo-ui datasoruce中编写了硬编码的JSON数据,并希望将其传输到Knockout observableArray。问题是toJSON似乎返回一个空数组,这对我来说并不明显,因为JSON在dataSource的数据中是硬编码的。

//The data hard-coded into the js-file
var eventsViewModel = new kendo.data.DataSource({
data:[
{   id:3,
    created:1385573907907,
    priority:1,
    reporter:"Martin",
    category:"Mähdrescher",
    type:"S690",
    title:"Hangausgleich defekt",
    message:"Mähdrescher umgekippt",
    status:"offen",
    client:"Bauer",
    field:"Kaiserslautern - Kohlbach",
    scenario:0,
    document:true,
    positionLatitude:49.405703, 
    positionLongitude :7.423623},

{   id:1,
    created:1385739507907,
    priority:3,
    reporter:"T670-4",
    category:"Mähdrescher",
    type:"T670",
    title:"Hydraulikdruck unter Minimalwert",
    message:"Hydraulikdruck unter 200 bar",
    status:"offen", 
    client:"Meyer", 
    field:"Homburg - Auf der Höh",
    scenario:0, 
    document :false, 
    positionLatitude:49.342852,
    positionLongitude:6.824287},
{   id:2,
    created:1385656707907,
    priority:2,
    reporter:"Anna",
    category:"Traktor",
    type:"9560RT",
    title:"Kette gerissen",
    message:"Linke Kette gerissen",
    status:"offen",
    client:"Schulze",
    field:"Kaiserslautern - Feldstraße",
    scenario:0, 
    document:false, 
    positionLatitude :49.402715, 
    positionLongitude :7.445082}
]
});
var json = eventsViewModel.data().toJSON();
console.log(JSON.stringify(json));

var events = ko.observableArray(eventsViewModel.data().toJSON());

ko.applyBindings(eventsViewModel,document.getElementById("event-view"));

一个JSbin,显示日志为空:http://jsbin.com/unEPAwAn/12/edit

1 个答案:

答案 0 :(得分:0)

在使用eventsViewModel.read();

访问数据之前,您需要调用eventsViewModel.data();(即使数据是本地绑定的)