Sencha ExtJS协会的帖子错了吗?

Sencha ExtJS协会的帖子错了吗?,extjs,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' }, { n

因此,我在使用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'
        }
    }
});
商店

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'
        }
    }
});
问题是我得到了这样一个帖子:

{
    "postTemplate": 1,
    "id": 0,
    "name": "foo"
}
但我需要这样的帖子:

{
    "postTemplate": {
        "id": 1,
        "blah": "foo"
    },
    "id": 0,
    "name": "bar"
}
另外,像“setPostTemplate”这样的评估函数不存在,我认为应该自动创建它

已经尝试过类似“record.data.postTemplate=(…)”的内容,但是我得到了一个无限循环,抛出了一个execption

有人能给点建议吗?另外,如果你需要其他有用的答案,请告诉我


谢谢

开箱即用关联对象不会按预期序列化回服务器。这个问题已经被多次提到。最好是遵循以下思路:

此线程讨论在JSON编写器类中重写getRecordData