0
"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"}
    }
}

I have nested json like above and I was really confused on how to write store root property and model fields..could anyone please help me on this

I tried giving rootProperty: content but it is not working

I want to call data in the view as {ups_ground.amount} does this works??

4

1 回答 1

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);

    }

});

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

*这段代码都未经测试,即使它不起作用,它也应该让你知道该怎么做。

于 2013-07-12T18:08:15.487 回答