Sencha ExtJS协会POST错吗?

时间:2012-11-20 17:52:39

标签: extjs

所以,我在使用Sencha ExtJs 4.1关联时遇到了问题。

我有类似的东西:

模型

Ext.define('Tpl.model.User', {
    extend: 'Ext.data.Model',
    requires: ['Tpl.model.PostTemplate'],
    fields: [
        { name: 'id', type: 'int' },
        { name: 'name', type: 'string' },
        { name: 'postTemplate', type: 'int' }
    ],
    associations: [
        { type: 'belongsTo', name: 'postTemplate', model:'Tpl.model.PostTemplate', associationKey: 'postTemplate', primaryKey: 'id', foreignKey: 'postTemplate' }
    ]
});

and

Ext.define('Tpl.model.PostTemplate', {
    extend: 'Ext.data.Model',

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

    associations: [
        { type: 'hasMany', model: 'Tpl.model.User' }
    ]

});

店铺

Ext.define('Tpl.store.Users', {
    extend: 'Ext.data.Store',
    model: 'Tpl.model.User',
    autoLoad: true,
    proxy: {
        type: 'rest',
        url: '../users',
        reader: {
            type: 'json',
            root: ''
        },
        writer: {
            type: 'json'
        }
    }
});

Ext.define('Tpl.store.PostTemplate', {
    extend: 'Ext.data.Store',
    model: 'Tpl.model.PostTemplate',
    autoLoad: true,
    proxy: {
        type: 'rest',
        //appendId: true,
        url: '../postTemplates/',
        reader: {
            type: 'json',
            root: ''
        },
        writer: {
            type: 'json'
        }
    }
});

问题是我正在接受这样的POST:

{
    "postTemplate": 1,
    "id": 0,
    "name": "foo"
}

但我需要这样的POST:

{
    "postTemplate": {
        "id": 1,
        "blah": "foo"
    },
    "id": 0,
    "name": "bar"
}

此外,评估员函数如“setPostTemplate”不存在,我认为它应该自动创建。

已经尝试过像“record.data.postTemplate =(...)”这样的东西但是我得到了一个无限循环抛出一个execption ......

有人可以提供建议吗?此外,如果您需要其他可能对答案有用的内容,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

开箱即用的关联对象未按预期序列化回服务器。这个问题已多次提出。最好的办法就是遵循这个主题:

http://www.sencha.com/forum/showthread.php?141957-Saving-objects-that-are-linked-hasMany-relation-with-a-single-Store

该线程讨论了在JSON编写器类中覆盖getRecordData。