2012-10-19 104 views
4

我现在用的是剑道UI组合框与外部数据源XML剑道UI DataSource对象。这里的数据源代码:缓存使用本地存储

try 
    { 
     var csDataSrc = new kendo.data.DataSource(
     { 
      transport: 
     { 
      read: "Data/StateList.xml", 
      dataType: "xml", 
      create: { cache: true } 
     }, 
     schema: 
     { 
      type: "xml", 
      data: "/States/State", 
      model: 
      { 
       fields: 
       { 
        id: "id/text()", 
        name: "name/text()" 
       } 
      } 
     } 
    }); 
    csDataSrc.read(); 
} 
catch (err) 
{ 
    log.error(err.message); 
} 

创建该数据源,这里是一个创建剑道组合框代码:

$("#stateList").kendoComboBox(
{ 
    index: 0, 
    placeholder: "Begin typing Coverage State...", 
    dataTextField: "name", 
    dataValueField: "id", 
    filter: "contains", 
    dataSource: csDataSrc, 
    text: $("#hdnStateName").val(), 
    value: $("#hdnStateKey").val(), 
    change: function(e) 
    { 
     $("#hdnStateKey").val(this.value()); 
     $("#hdnStateName").val(this.text()); 
    } 
}); 

这个作品真的很好,但对于真正的列表中的数据是巨大的,我倒要它像这样的东西存储在本地存储: localStorage.setItem(“state_key”,csDataSrc); 然后当页面加载,而不是建设,并从服务器端XML阅读所有的时间,我想为它是这样的:

var csDataSrc = localStorage.getItem("state_key"); 
if(csDataSrc === null) 
{ 
    // create the data source with the above code 
    // and store it in localStorage. 
} 

然后将其绑定在这里......

...kendoComboBox(
{ 
    ..., 
    .dataSource: csDataSrc, 
    ... 
}); 

我创建数据源精细,它似乎正确地存储在localStorage的,但是当你离开页面,回来的数据源总是空。我可以看到它使用的Chrome开发人员工具的资源选项卡,但它不会绑定到组合框正确。 任何帮助,或者如果我需要阐述什么,使之更清楚,请让我知道

感谢 -s

回答

10

为了实现这一目标,您可以使用自定义的阅读方法(link)。

transport: { 
    read: function(operation) { 
     var cashedData = localStorage.getItem("moviesData"); 

     if(cashedData != null || cashedData != undefined) { 
      //if local data exists load from it 
      operation.success(JSON.parse(cashedData)); 
     } else { 
      $.ajax({ //using jsfiddle's echo service to simulate remote data loading 
       url: '/echo/json/', 
       type: "POST", 
       dataType: "json", 
       data: { 
        json: JSON.stringify(data) 
       }, 
       success: function(response) { 
        //store response 
        localStorage.setItem("moviesData", JSON.stringify(response)); 
        //pass the pass response to the DataSource 
        operation.success(response); 
       } 
      }); 
     }     
    } 
} 

这里是一个工作示例:http://jsfiddle.net/LnTe7/12/