【问题标题】:Caching a Kendo UI DataSource object using localStorage使用 localStorage 缓存 Kendo UI DataSource 对象
【发布时间】:2012-10-19 18:39:21
【问题描述】:

我正在使用带有外部 XML 数据源的 Kendo UI ComboBox。这是数据源代码:

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

【问题讨论】:

    标签: local-storage kendo-ui


    【解决方案1】:

    要实现这一点,您可以使用自定义读取方法 (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/

    【讨论】:

      猜你喜欢
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多