是Sencha ExtJS关联POST错误吗? [英] Is Sencha ExtJS association POST wrong?
问题描述
所以,我使用Sencha ExtJs 4.1关联问题。
So, I have a problem using Sencha ExtJs 4.1 Associations.
我有一些类似的东西:
模型
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:
The problem is that I'm getting a POST like this:
{
"postTemplate": 1,
"id": 0,
"name": "foo"
}
但是我需要一个这样的POST:
But I need a POST like this:
{
"postTemplate": {
"id": 1,
"blah": "foo"
},
"id": 0,
"name": "bar"
}
此外,评估者功能(如setPostTemplate)不存在,我认为应该自动创建。
Also, the assessor function like "setPostTemplate" doesn't exist and I think it should be created automatically.
已经尝试过像记录.data.postTemplate =(...)但是我有一个无限循环抛出一个执行...
Already tried to something like " record.data.postTemplate = (...) " but I got an infinite loop throwing an execption...
有人可以告诉你吗?另外,如果你需要其他可能对答案有用的东西,让我知道。
Can someone please advise? Also, if you need something else that could be useful for the answer let me know.
谢谢!
推荐答案
开箱即用的关联对象不会像您所期望的那样序列化回服务器。这个问题已经提出了很多次。最好的事情可能是按照这个线程:
Out of the box the Associated objects are not serialized back to the server as you expect them . This issue has been brought up many times. The best thing might be to follow this thread:
此线程谈论在JSON writer类中覆盖getRecordData。
This thread talks about overriding getRecordData in the JSON writer class.
这篇关于是Sencha ExtJS关联POST错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!