PDA

View Full Version : Record object property mapping



bkraut
18 Sep 2009, 2:40 AM
Hi,

I'm trying to set an object with a structure:



var MetaDocumentType = Ext.data.Record.create([
{name: 'id', type: 'int', allowBlank: false},
{name: 'name', type: 'string', allowBlank: false},
{name: 'nameSymbol', type: 'string', allowBlank: false},
{name: 'metaClassId', type: 'int', allowBlank: false, mapping: 'docTypeMetaClass.id'}
]);


like this:



new MetaDocumentType(this.treeNode.attributes.attributes[0].record)


except docTypeMetaClass.id all fields are populated.

I'm attaching an object debug structure in FF.

Any help appreciated.

Condor
18 Sep 2009, 2:51 AM
Your code should work!

Are you sure you posted the correct object and you checked the record for metaClassId (and not docTypeMetaClass.id)?

ps. If the docTypeMetaClass object is optional you should use:

var MetaDocumentType = Ext.data.Record.create([
{name: 'id', type: 'int', allowBlank: false},
{name: 'name', type: 'string', allowBlank: false},
{name: 'nameSymbol', type: 'string', allowBlank: false},
{name: 'metaClassId', type: 'int', allowBlank: false, mapping: 'docTypeMetaClass', convert: function(v){
return v ? v.id : null;
}}
]);

bkraut
18 Sep 2009, 3:26 AM
Hi,
thanks for the info - I applied your patch.
Anyway - still it doesn't work.

If I do this

console.log(node.data.docTypeMetaClass.id);

I get an value

for

console.log(node.data.metaClassId);
I get undefined.

Condor
18 Sep 2009, 3:31 AM
You are testing wrong:

console.log(data.docTypeMetaClass.id);
var record = new MetaDocumentType(data);
console.log(record.get('metaClassId'));

bkraut
18 Sep 2009, 3:47 AM
Ok. It works now. I get a number. Thanks.

I can set now all the values outside of the class, but not inside.

My case is - I'm subclassing a window for specific record like here for example:



Ext.namespace("Ext.ux");

Ext.ux.MetaDocumentTypeWindow = Ext.extend(Ext.Window, {

// ***********************************************************************************
// ***********************************************************************************
// Init component
// ***********************************************************************************
// ***********************************************************************************

initComponent: function() {

this.form = this.getFormDefinition();

Ext.apply(this, {
title: 'Document Type Form',
width:550,
height:190,
iconCls: 'documentType',
shim:false,
animCollapse: true,
constrainHeader:true,
layout:'fit',
items: [
this.form
]
});

this.on({
show: function() {
if (this.params && this.params.id) {
this.form.getForm().load({
url: 'Controller/Meta/metadocumenttype.Load',
params: {
id: this.params.id
},
waitMsg:'Loading...',
success: function(action, responseData) {

},
failure: function(action, responseData) {
Ext.MessageBox.alert ("Error", Ext.decode(responseData.response.responseText).message);
}
});
} else {
var object = new MetaDocumentType({
metaClassId: this.params.metaClassId
});
this.form.getForm().loadRecord(object);
}
},
scope: this
});


// ***********************************************************************************
// superClass initComponent
// ***********************************************************************************

Ext.ux.MetaDocumentTypeWindow.superclass.initComponent.apply(this, arguments);

},



// ***********************************************************************************
// ***********************************************************************************
// OnRender component
// ***********************************************************************************
// ***********************************************************************************

onRender: function() {
Ext.ux.MetaDocumentTypeWindow.superclass.onRender.apply(this, arguments);
},

getFormDefinition: function() {
return new Ext.ux.ExtendedFormPanel({
url: 'Controller/Meta/metadocumenttype.Save',
items: [{
xtype: "fieldset",
title: "Document Type information",
autoHeight: true,
items: [{
xtype: 'hidden',
name: 'id'
},{
xtype: 'textfield',
name: 'metaClassId'
}, {
xtype: 'textfield',
fieldLabel: 'Name Symbol',
name: 'nameSymbol',
width: 40
}, {
xtype: 'textfield',
fieldLabel: 'Name',
name: 'name',
width: 375
}]
}],
listeners: {
cancel: function() {
this.close();
},
submit: function() {
if (!this.form.getForm().isValid()) {
App.setAlert(false, "Form is invalid");
return false;
} else {
this.form.getForm().submit({
waitMsg:'Saving ...',
success: function(response,recordData) {
this.close();
}.createDelegate(this),
failure : function(action, responseData) {
Ext.MessageBox.alert ("Error", Ext.decode(responseData.response.responseText).message);
},
scope: this.form
});
}
},
scope: this
}
});
}
});

Ext.reg('MetaDocumentTypeWindow', Ext.ux.MetaDocumentTypeWindow);


The same window can be called with an Id or without Id. If there is no Id given I create a new record when submitting, but in case if Id exists, I update the record.

The problem is, when I use


show: function() {
if (this.params && this.params.id) {
this.form.getForm().load({
url: 'Controller/Meta/metadocumenttype.Load',
params: {
id: this.params.id
},
waitMsg:'Loading...',
success: function(action, responseData) {

},
failure: function(action, responseData) {
Ext.MessageBox.alert ("Error", Ext.decode(responseData.response.responseText).message);
}
});
} else {
var object = new MetaDocumentType({
metaClassId: this.params.metaClassId
});
this.form.getForm().loadRecord(object);
}
},


loading when window is opened, metaClassId is not populated, but other fields are. Why?

bkraut
18 Sep 2009, 3:51 AM
Accidentally I created another post.

Anyway - my problem is that I'm subclassing windows for each different record - so in my case I call a window with or without an id (dependes if I need to create a new record or update existing one).

Here is an example of such class:




Ext.namespace("Ext.ux");

Ext.ux.MetaDocumentTypeWindow = Ext.extend(Ext.Window, {

// ***********************************************************************************
// ***********************************************************************************
// Init component
// ***********************************************************************************
// ***********************************************************************************

initComponent: function() {

this.form = this.getFormDefinition();

Ext.apply(this, {
title: 'Document Type Form',
width:550,
height:190,
iconCls: 'documentType',
shim:false,
animCollapse: true,
constrainHeader:true,
layout:'fit',
items: [
this.form
]
});

this.on({
show: function() {
if (this.params && this.params.id) {
this.form.getForm().load({
url: 'Controller/Meta/metadocumenttype.Load',
params: {
id: this.params.id
},
waitMsg:'Loading...',
success: function(action, responseData) {

},
failure: function(action, responseData) {
Ext.MessageBox.alert ("Error", Ext.decode(responseData.response.responseText).message);
}
});
} else {
var object = new MetaDocumentType({
metaClassId: this.params.metaClassId
});
this.form.getForm().loadRecord(object);
}
},
scope: this
});


// ***********************************************************************************
// superClass initComponent
// ***********************************************************************************

Ext.ux.MetaDocumentTypeWindow.superclass.initComponent.apply(this, arguments);

},



// ***********************************************************************************
// ***********************************************************************************
// OnRender component
// ***********************************************************************************
// ***********************************************************************************

onRender: function() {
Ext.ux.MetaDocumentTypeWindow.superclass.onRender.apply(this, arguments);
},

getFormDefinition: function() {
return new Ext.ux.ExtendedFormPanel({
url: 'Controller/Meta/metadocumenttype.Save',
items: [{
xtype: "fieldset",
title: "Document Type information",
autoHeight: true,
items: [{
xtype: 'hidden',
name: 'id'
},{
xtype: 'textfield',
name: 'metaClassId'
}, {
xtype: 'textfield',
fieldLabel: 'Name Symbol',
name: 'nameSymbol',
width: 40
}, {
xtype: 'textfield',
fieldLabel: 'Name',
name: 'name',
width: 375
}]
}],
listeners: {
cancel: function() {
this.close();
},
submit: function() {
if (!this.form.getForm().isValid()) {
App.setAlert(false, "Form is invalid");
return false;
} else {
this.form.getForm().submit({
waitMsg:'Saving ...',
success: function(response,recordData) {
this.close();
}.createDelegate(this),
failure : function(action, responseData) {
Ext.MessageBox.alert ("Error", Ext.decode(responseData.response.responseText).message);
},
scope: this.form
});
}
},
scope: this
}
});
}
});

Ext.reg('MetaDocumentTypeWindow', Ext.ux.MetaDocumentTypeWindow);



as you see I'm using on show, as I need to open a window from anywhere from the system. The case is when show fnction is triggered form values are populated, except from the metaClassId field.

Why?

bkraut
18 Sep 2009, 1:56 PM
Thanks this works.
Still - I'm using form.load() on window show event - and there all the fields are populated correctly except metaClassId.

I submited the code twice, but I always get taht my post will be published when moderator approves it.?????