在数据对象中添加项目

Add items in data object

你好,我有这种格式的数据:

{
    "data": [
        {
            "id": 1,
            "title": "some title",
            "body": "some body",
            "coordinate": [
                {
                   "id": 1,
                    "x": 10,
                    "y": 20,
                    "z": 30
                },
                {
                   "id": 2,
                    "x": 30,
                    "y": 20,
                    "z": 30
                },
                {
                   "id": 3,
                    "x": 20,
                    "y": 30,
                    "z": 30
                },
                {
                   "id": 4,
                    "x": 50,
                    "y": 40,
                    "z": 20
                }
            ]
        }
    ]
}

并拥有这个数据模型:

Ext.define('Book.model.Shops', {
    extend: 'Ext.data.Model',

    fields: [
        { name: 'id', type: 'int' },
        { name: 'title', type: 'string' },
        { name: 'body', type: 'string' },
        { name: 'coordinate', type: 'auto' },
    ]
})

我想在 "coordinate" 对象内的当前记录中添加新项目,但我不知道该怎么做。 当添加新记录时,我是这样的:

       newRec = {
                title: newTitle,
                body: newBody,
                };
            store.add(newRec)

但是如何在对象中添加项目到当前记录"coordinate"&

你可以使用很多这样的模型

Ext.define("Book.model.Cordinate", { 扩展:'Ext.data.Model',

        fields: [
            { name: 'id', type: 'int' },
            { name: 'y', type: 'string' },
            { name: 'y', type: 'string' },
            { name: 'z', type: 'string' }               
        ]

    });


    Ext.define('Book.model.Shops', {
        extend: 'Ext.data.Model',

        fields: [
            { name: 'id', type: 'int' },
            { name: 'title', type: 'string' },
            { name: 'body', type: 'string' }               
        ],
        hasMany: {model: 'Book.model.Cordinate', name: 'coordinate'}              
    });


 var shops = Ext.create('Book.model.Shops', {id: 1, title: 'some title'});

var coordinate = Ext.create('Book.model.Cordinate', {id: 1, y: 'test'});

var lcoordinate = shops.coordinate();

lcoordinate.add(coordinate);

Ext.Msg.alert(shops.get('title'));

Ext.Msg.alert(shops.coordinate().first().get('y'));

http://docs.sencha.com/extjs/4.1.2/#!/api/Ext.data.association.HasMany