【问题标题】:Is Sencha ExtJS association POST wrong?Sencha ExtJS 关联 POST 错了吗?
【发布时间】:2012-11-20 17:52:38
【问题描述】:

所以,我在使用 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'
        }
    }
});

问题是我收到这样的帖子:

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

但我需要这样的 POST:

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

另外,像“setPostTemplate”这样的评估函数不存在,我认为它应该是自动创建的。

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

有人可以建议吗?另外,如果您需要其他可能对答案有用的东西,请告诉我。

谢谢!

【问题讨论】:

    标签: extjs


    【解决方案1】:

    开箱即用的关联对象不会像您期望的那样序列化回服务器。这个问题已经被提过很多次了。最好的办法可能是关注这个帖子:

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

    本主题讨论了在 JSON 编写器类中覆盖 getRecordData。

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 2013-03-14
      相关资源
      最近更新 更多