5

尝试(不成功)显示来自嵌套 json 的数据。

JSON 可能类似于:

{
    "contacts": [
        {
            "id": "1",
            "client_id": "135468714603",
            "addresses": [
                {
                    "id": "1",
                    "contact_id": "1",
                    "address_id": "16",
                    "address": {
                        "0": {
                            "id": "16",
                            "address": "123 Some Rd",
                            "address2": "",
                            "city": "Toen",
                            "state": "VS",
                            "zip_code": "11111",
                            "country": "USA"
                        }
                    }
                },
                {
                    "id": "6",
                    "contact_id": "1",
                    "address_id": "26",
                    "address": {
                        "0": {
                            "id": "26",
                            "address": "1 Other Road",
                            "address2": "",
                            "city": "Twn",
                            "state": "BD",
                            "zip_code": "11112",
                            "country": "USA"
                        }
                    }
                }
            ]
        },
        {
            "id": "10",
            "client_id": null,
            "addresses": [
                {
                    "id": "8",
                    "contact_id": "10",
                    "address_id": "28",
                    "address": {
                        "0": {
                            "id": "28",
                            "address": "54 Road",
                            "address2": "",
                            "city": "TWND",
                            "state": "TT",
                            "zip_code": "11113",
                            "country": "USA"
                        }
                    }
                },
                {
                    "id": "9",
                    "contact_id": "10",
                    "address_id": "29",
                    "is_mailing_address": "0",
                    "is_primary_address": "0",
                    "display_priority": "0",
                    "address": {
                        "0": {
                            "id": "29",
                            "address": "6 Road",
                            "address2": "",
                            "city": "TOEOEOWN",
                            "state": "PY",
                            "zip_code": "11116",
                            "country": "USA"
                        }
                    }
                },
                {
                    "id": "10",
                    "contact_id": "10",
                    "address_id": "30",
                    "address": {
                        "0": {
                            "id": "30",
                            "address": "PO Box 9",
                            "address2": "",
                            "city": "TOYN",
                            "state": "GF",
                            "zip_code": "11118",
                            "country": "USA"
                        }
                    }
                }
            ]
        },
        {
            "id": "11",
            "client_id": null,
            "contact_id": "11",
            "addresses": [
                {
                    "id": "11",
                    "contact_id": "11",
                    "address_id": "33",
                    "is_mailing_address": "0",
                    "is_primary_address": "0",
                    "display_priority": "0",
                    "address": {
                        "0": {
                            "id": "33",
                            "address": "4 Street",
                            "address2": "",
                            "city": "TEOIN",
                            "state": "TG",
                            "zip_code": "11119",
                            "country": "USA"
                        }
                    }
                }
            ]
        }
    ]
}

我已经尝试将模型字段映射到我需要的内容(例如联系人模型>地址字段>映射:地址),但这不起作用,因为我需要映射到addresses[0].address[0]来获取显然丢弃其他地址的数据。

我也尝试过使用关联,但这似乎是单独的模型和商店。这里的想法是不要单独请求联系人,然后是他们的地址。

我还尝试直接在模板中挖掘 json(这似乎是最直接的方法),例如 {addresses.address.city} 不起作用。

思路很简单:抓取一些 json,并在应用程序的不同部分显示所述 json 的不同部分。

经历是可怕的。

有人可以解释如何映射这些嵌套的 json 项,以便可以从模板访问它们吗?

模板:

{
    xtype: 'container',
    flex: 1,
    id: 'mainPanel',
    items: [
        {
            xtype: 'dataview',
            hidden: false,
            id: 'clientsContacts',
            minHeight: 200,
            itemSelector: 'div',
            itemTpl: [
                '{id} | {last_name} | {first_name} | {relationship} | {addresses}'
            ],
            store: 'Contacts'
        }
    ]
}

店铺:

Ext.define('MyApp.store.Contacts', {
    extend: 'Ext.data.Store',

    requires: [
        'MyApp.model.Contacts'
    ],

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            autoLoad: false,
            storeId: 'Contacts',
            model: 'MyApp.model.Contacts',
            proxy: {
                type: 'ajax',
                extraParams: {
                    id: '',
                    format: 'json'
                },
                url: '/api/contacts/', //the json
                reader: {
                    type: 'json',
                    root: 'contacts'
                }
            },
            listeners: {
                load: {
                    //fn: me.onJsonstoreLoad,
                    //scope: me
                }
            }
        }, cfg)]);
    },
});

模型:

Ext.define('MyApp.model.Contacts', { 扩展:'Ext.data.Model',

    uses: [
        //'MyApp.model.Client',
        //'MyApp.model.contactAddressModel'
    ],

    fields: [
        {
            name: 'client_id'
        },
        {
            name: 'id'
        },
        {
            name: 'addresses',
            mapping: 'addresses'//doesn't work
            //mapping: 'addresses[0].address[0]' //works, but only for the first address duh
        }
    ],
});

通过 Sencha Architect 使用 extjs 4.1。

任何帮助将不胜感激。

谢谢。

4

2 回答 2

4

我想我明白了(希望它是正确的)。

因此,为您需要的每个嵌套数据组创建一个字段。所以我有一个联系人模型。在该模型中有以下字段:

id
client_id
addresses //mapped to addresses
address //mapped to addresses.address 

然后在模板中:

<br>
<tpl for="addresses">
    id: {id}<br>
    addy id: {address_id}<br>

    <tpl for="address">
        {city} {state}, {zip}<br>
    </tpl>

</tpl>

这是整个事情的样子:

看法

Ext.define('MyApp.view.MyView', {
    extend: 'Ext.view.View',

    height: 250,
    width: 400,
    itemSelector: 'div',
    store: 'MyJsonStore',

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            itemTpl: [
                '<br>',
                '<tpl for="addresses">',
                '    id: {id}<br>',
                '    addy id: {address_id}<br>',
                '    <b>',
                '    <tpl for="address">',
                '        {city} {state}, {zip}<br><br>',
                '    </tpl>',
                '    </b>',
                '',
                '</tpl>',
                '',
                '<hr>',
                ''
            ]
        });

        me.callParent(arguments);
    }

});

店铺

Ext.define('MyApp.store.MyJsonStore', {
    extend: 'Ext.data.Store',

    requires: [
        'MyApp.model.Contacts'
    ],

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            storeId: 'MyJsonStore',
            model: 'MyApp.model.Contacts',
            data: {
                contacts: [
                    {
                        id: '1',
                        client_id: '135468714603',
                        addresses: [
                            {
                                id: '1',
                                contact_id: '1',
                                address_id: '16',
                                address: {
                                    '0': {
                                        id: '16',
                                        address: '123 Some Rd',
                                        address2: '',
                                        city: 'Toen',
                                        state: 'VS',
                                        zip_code: '11111',
                                        country: 'USA'
                                    }
                                }
                            },
                            {
                                id: '6',
                                contact_id: '1',
                                address_id: '26',
                                address: {
                                    id: '26',
                                    address: '1 Other Road',
                                    address2: '',
                                    city: 'Twn',
                                    state: 'BD',
                                    zip_code: '11112',
                                    country: 'USA'
                                }
                            }
                        ]
                    },
                    {
                        id: '10',
                        client_id: null,
                        addresses: [
                            {
                                id: '8',
                                contact_id: '10',
                                address_id: '28',
                                address: {
                                    id: '28',
                                    address: '54 Road',
                                    address2: '',
                                    city: 'TWND',
                                    state: 'TT',
                                    zip_code: '11113',
                                    country: 'USA'
                                }
                            },
                            {
                                id: '9',
                                contact_id: '10',
                                address_id: '29',
                                is_mailing_address: '0',
                                is_primary_address: '0',
                                display_priority: '0',
                                address: {
                                    id: '29',
                                    address: '6 Road',
                                    address2: '',
                                    city: 'TOEOEOWN',
                                    state: 'PY',
                                    zip_code: '11116',
                                    country: 'USA'
                                }
                            },
                            {
                                id: '10',
                                contact_id: '10',
                                address_id: '30',
                                address: {
                                    id: '30',
                                    address: 'PO Box 9',
                                    address2: '',
                                    city: 'TOYN',
                                    state: 'GF',
                                    zip_code: '11118',
                                    country: 'USA'
                                }
                            }
                        ]
                    },
                    {
                        id: '11',
                        client_id: null,
                        contact_id: '11',
                        addresses: [
                            {
                                id: '11',
                                contact_id: '11',
                                address_id: '33',
                                is_mailing_address: '0',
                                is_primary_address: '0',
                                display_priority: '0',
                                address: {
                                    id: '33',
                                    address: '4 Street',
                                    address2: '',
                                    city: 'TEOIN',
                                    state: 'TG',
                                    zip_code: '11119',
                                    country: 'USA'
                                }
                            }
                        ]
                    }
                ]
            },
            proxy: {
                type: 'memory',
                reader: {
                    type: 'json',
                    root: 'contacts'
                }
            }
        }, cfg)]);
    }
});

模型

Ext.define('MyApp.model.Contacts', {
    extend: 'Ext.data.Model',

    fields: [
        {
            name: 'id'
        },
        {
            name: 'client_id'
        },
        {
            name: 'addresses',
            mapping: 'addresses'
        },
        {
            name: 'address',
            mapping: 'address'
        }
    ]
});
于 2013-03-06T19:46:18.443 回答
1

我已经验证了上面的答案确实有效,但请注意,如果您不指定子字段的名称,则不需要第二个嵌套模板。你可以用第一个来做。

于 2014-03-24T14:11:55.327 回答