admin管理员组文章数量:1023025
I have a JSON like this
{
"success": true,
"users": [{
"name":"Boom",
"emails": [{
"first": "[email protected]",
"second": "[email protected]",
"countries":[{
"label":"pakistan",
"continent":"asia"
}]
}]
}]
}
I have created my models for it like this
Ext.define('WR.model.WorkRecord', {
extend: 'Ext.data.Model',
fields: ['name'],
hasMany: {model: 'WR.model.Email', name: 'emails'}
});
Ext.define('WR.model.Email', {
extend: 'Ext.data.Model',
fields: ['first', 'second'],
belongsTo: {model : 'WR.model.WorkRecord', name: 'users'},
hasMany: {model: 'WR.model.Countries', name: 'countries'}
});
Ext.define('WR.model.Countries', {
extend: 'Ext.data.Model',
fields: ['label', 'continent'],
belongsTo: {model: 'WR.model.Email', name: 'emails'}
});
Now I want to populate my form having id formJobSummary
.I did it successfuly for Non-Nested JSON by calling this function in store
listeners: {
load: function(users) {
var form = Ext.getCmp('formJobSummary');
form.loadRecord(this.data.first());
}
}
My form has just simple displayfields and I want to populate them through this nested JSON thanks
I have a JSON like this
{
"success": true,
"users": [{
"name":"Boom",
"emails": [{
"first": "[email protected]",
"second": "[email protected]",
"countries":[{
"label":"pakistan",
"continent":"asia"
}]
}]
}]
}
I have created my models for it like this
Ext.define('WR.model.WorkRecord', {
extend: 'Ext.data.Model',
fields: ['name'],
hasMany: {model: 'WR.model.Email', name: 'emails'}
});
Ext.define('WR.model.Email', {
extend: 'Ext.data.Model',
fields: ['first', 'second'],
belongsTo: {model : 'WR.model.WorkRecord', name: 'users'},
hasMany: {model: 'WR.model.Countries', name: 'countries'}
});
Ext.define('WR.model.Countries', {
extend: 'Ext.data.Model',
fields: ['label', 'continent'],
belongsTo: {model: 'WR.model.Email', name: 'emails'}
});
Now I want to populate my form having id formJobSummary
.I did it successfuly for Non-Nested JSON by calling this function in store
listeners: {
load: function(users) {
var form = Ext.getCmp('formJobSummary');
form.loadRecord(this.data.first());
}
}
My form has just simple displayfields and I want to populate them through this nested JSON thanks
Share Improve this question asked Aug 6, 2012 at 13:36 WaseemWaseem 1,4086 gold badges23 silver badges30 bronze badges 6-
If you have
hasMany
relationship in your model, how can the form know how many fields to prepare? Given your model definition, a work record can have many emails... – Izhaki Commented Aug 6, 2012 at 13:45 - Actually I am a beginner and don't have any idea how to implement it – Waseem Commented Aug 6, 2012 at 13:59
-
Well the first question will be whether
hasMany
relationship is correct here: can a work record have more than one email (each email has first and second fields)? – Izhaki Commented Aug 6, 2012 at 14:01 - lets suppose it can have – Waseem Commented Aug 7, 2012 at 2:05
- Well in that case a form won't help you. You'd also need to display a grid with as many emails as a work record might have. This is not extremely hard to do, but you'd need to explicitly have a grid (not part of a form) and explicitly load the emails to it. – Izhaki Commented Aug 7, 2012 at 2:11
2 Answers
Reset to default 4Currently you can't use name='property.subProperty' in a form field definition :(.
So in order to make this work, I revert the logic - add (a redundant) field definition to model:
Ext.define('WR.model.WorkRecord', {
extend: 'Ext.data.Model',
fields: [
'name',
{name: 'emailFirst', mapping: 'emails.first'}
],
hasMany: {model: 'WR.model.Email', name: 'emails'}
});
then you can create a form field like:
{
xtype: 'displayfield',
name: 'emailFirst',
...
}
And it will be populated on form.loadRecord()
You need to subclass your Store and add the requires config.
Ext.define('MyJsonStore', {
extend: 'Ext.data.JsonStore',
requires: [
'WR.model.WorkRecord'
]
});
I have a JSON like this
{
"success": true,
"users": [{
"name":"Boom",
"emails": [{
"first": "[email protected]",
"second": "[email protected]",
"countries":[{
"label":"pakistan",
"continent":"asia"
}]
}]
}]
}
I have created my models for it like this
Ext.define('WR.model.WorkRecord', {
extend: 'Ext.data.Model',
fields: ['name'],
hasMany: {model: 'WR.model.Email', name: 'emails'}
});
Ext.define('WR.model.Email', {
extend: 'Ext.data.Model',
fields: ['first', 'second'],
belongsTo: {model : 'WR.model.WorkRecord', name: 'users'},
hasMany: {model: 'WR.model.Countries', name: 'countries'}
});
Ext.define('WR.model.Countries', {
extend: 'Ext.data.Model',
fields: ['label', 'continent'],
belongsTo: {model: 'WR.model.Email', name: 'emails'}
});
Now I want to populate my form having id formJobSummary
.I did it successfuly for Non-Nested JSON by calling this function in store
listeners: {
load: function(users) {
var form = Ext.getCmp('formJobSummary');
form.loadRecord(this.data.first());
}
}
My form has just simple displayfields and I want to populate them through this nested JSON thanks
I have a JSON like this
{
"success": true,
"users": [{
"name":"Boom",
"emails": [{
"first": "[email protected]",
"second": "[email protected]",
"countries":[{
"label":"pakistan",
"continent":"asia"
}]
}]
}]
}
I have created my models for it like this
Ext.define('WR.model.WorkRecord', {
extend: 'Ext.data.Model',
fields: ['name'],
hasMany: {model: 'WR.model.Email', name: 'emails'}
});
Ext.define('WR.model.Email', {
extend: 'Ext.data.Model',
fields: ['first', 'second'],
belongsTo: {model : 'WR.model.WorkRecord', name: 'users'},
hasMany: {model: 'WR.model.Countries', name: 'countries'}
});
Ext.define('WR.model.Countries', {
extend: 'Ext.data.Model',
fields: ['label', 'continent'],
belongsTo: {model: 'WR.model.Email', name: 'emails'}
});
Now I want to populate my form having id formJobSummary
.I did it successfuly for Non-Nested JSON by calling this function in store
listeners: {
load: function(users) {
var form = Ext.getCmp('formJobSummary');
form.loadRecord(this.data.first());
}
}
My form has just simple displayfields and I want to populate them through this nested JSON thanks
Share Improve this question asked Aug 6, 2012 at 13:36 WaseemWaseem 1,4086 gold badges23 silver badges30 bronze badges 6-
If you have
hasMany
relationship in your model, how can the form know how many fields to prepare? Given your model definition, a work record can have many emails... – Izhaki Commented Aug 6, 2012 at 13:45 - Actually I am a beginner and don't have any idea how to implement it – Waseem Commented Aug 6, 2012 at 13:59
-
Well the first question will be whether
hasMany
relationship is correct here: can a work record have more than one email (each email has first and second fields)? – Izhaki Commented Aug 6, 2012 at 14:01 - lets suppose it can have – Waseem Commented Aug 7, 2012 at 2:05
- Well in that case a form won't help you. You'd also need to display a grid with as many emails as a work record might have. This is not extremely hard to do, but you'd need to explicitly have a grid (not part of a form) and explicitly load the emails to it. – Izhaki Commented Aug 7, 2012 at 2:11
2 Answers
Reset to default 4Currently you can't use name='property.subProperty' in a form field definition :(.
So in order to make this work, I revert the logic - add (a redundant) field definition to model:
Ext.define('WR.model.WorkRecord', {
extend: 'Ext.data.Model',
fields: [
'name',
{name: 'emailFirst', mapping: 'emails.first'}
],
hasMany: {model: 'WR.model.Email', name: 'emails'}
});
then you can create a form field like:
{
xtype: 'displayfield',
name: 'emailFirst',
...
}
And it will be populated on form.loadRecord()
You need to subclass your Store and add the requires config.
Ext.define('MyJsonStore', {
extend: 'Ext.data.JsonStore',
requires: [
'WR.model.WorkRecord'
]
});
本文标签: javascriptHow to populate Form with nested JSON in Extjs 4Stack Overflow
版权声明:本文标题:javascript - How to populate Form with nested JSON in Extjs 4 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745502367a2153451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论