我有嵌套json,如何编写模型和存储? sencha touch 2

时间:2013-07-12 14:25:58

标签: extjs sencha-touch-2

"content":{"ups_ground":
                {"amount":"7.06",
                 "currency_code":"USD",
                 "data":[],
                 "tnt":"Monday, 7\/15 at 11:00pm"},

          "ups_next_day_air_saver":
                 {"amount":"26.44",
                  "currency_code":"USD",
                  "data":[],
                  "tnt":"Monday, 7\/15 at 3:00pm"},

          "ups_next_day_early_a.m.":
                {"amount":"63.84",
                 "currency_code":"USD",
                 "data":[],
                 "tnt":"Monday, 7\/15 at 8:30am"},

           "ups_next_day_air":
                     {"amount":"30.99",
                      "currency_code":"USD",
                      "data":[],"tnt":
                      "Monday, 7\/15 at 10:30am"}
    }
}

我已经像上面那样嵌套了json,我对如何编写商店根属性和模型字段感到困惑。有人可以帮我解决这个问题

我尝试过提供rootProperty:内容,但它无效

我想在视图中调用数据{ups_ground.amount}这是否有效?

1 个答案:

答案 0 :(得分:0)

您可以将服务器结构更改为标准的extjs json商店格式吗?如果是这样,您的商店设置将非常简单:

{
    "content":[
        {
            "shipping_type":"ups_ground",
            "amount":"7.06",
            "currency_code":"USD",
            "data":[],
            "tnt":"Monday, 7\/15 at 11:00pm"
        },
        {
            "shipping_type":"ups_next_day_air_saver",
            "amount":"26.44",
            "currency_code":"USD",
            "data":[],
            "tnt":"Monday, 7\/15 at 3:00pm"
        },
        {
            "shipping_type":"ups_next_day_early_a.m.",
            "amount":"63.84",
            "currency_code":"USD",
            "data":[],
            "tnt":"Monday, 7\/15 at 8:30am"
        },
        {
            "shipping_type":"ups_next_day_air",
            "amount":"30.99",
            "currency_code":"USD",
            "data":[],"tnt":
            "Monday, 7\/15 at 10:30am"
        }
    ]
}

你可以像这样声明模型和存储:

Ext.define("ShippingModel", {
    extend: "Ext.data.Model",
    fields: [
        {name:"shipping_type", type:"string"},
        {name:"amount",        type:"float"},
        {name:"currency_code", type:"string"},
        {name:"data",          type:"string"},
        {name:"tnt",           type:"string"}//this may need to be a date, lookup date format for this yourself
    ]
});

Ext.create('Ext.data.Store', {
    model: 'ShippingModel',
    controller: 'news',
    proxy: {
        type: 'ajax',
        url: 'your url',
        reader: {
            type: 'json',
            root: 'content'
        },
        extraParams:{
            action:'getShippingRates'//you may not need the extraParams, just putting here for example
        }
    },
    autoLoad: true
});

如果你不能像那样做那个标准,你可以扩展阅读器类来阅读你的自定义数据结构。它看起来像这样:

Ext.define("MyWeirdlyStucturedDataReader", {
    extend: "Ext.data.reader.Json",

    extractData: function(root){

        var newStructure = [];

        for(var shippingType in root)
        {
            var newRecord = root[shippingType];
            newRecord.shipping_type = shippingType;
            newStructure.push(newRecord);
        }

        return this.callParent(newStructure);

    }

});

然后,您将代理中的reader类型属性设置为“MyWeirdlyStucturedDataReader”。

*此代码未经测试,即使不起作用,也应该让您知道该怎么做。