View Full Version : [2.2] Data Record Mappings doesn't work
letssurf
22 Aug 2008, 11:19 AM
The following code was an example taken from the docs.
It will alert 'undefined' instead of 'Do my job please'
var TopicRecord = Ext.data.Record.create([
{name: 'title', mapping: 'topic_title'},
{name: 'author', mapping: 'username'},
{name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
{name: 'lastPost', mapping: 'post_time', type: 'date'},
{name: 'lastPoster', mapping: 'user2'},
{name: 'excerpt', mapping: 'post_text'}
]);
var myNewRecord = new TopicRecord({
topic_title: 'Do my job please',
username: 'noobie',
topic_replies: 1,
post_time: new Date(),
user2: 'Animal',
post_text: 'No way dude!'
});
alert(myNewRecord.get('title'));
Fix supplied by Animal
Ext.override(Ext.data.Record, {
get : function(name){
return this.data[this.fields.get(name).mapping];
},
set : function(name, value){
if(String(this.data[this.fields.get(name).mapping]) == String(value)){
return;
}
this.dirty = true;
if(!this.modified){
this.modified = {};
}
if(typeof this.modified[name] == 'undefined'){
this.modified[name] = this.data[name];
}
this.data[name] = value;
if(!this.editing && this.store){
this.store.afterEdit(this);
}
},
});
mystix
22 Aug 2008, 10:49 PM
original discussion can be found here:
http://extjs.com/forum/showthread.php?t=40904
Condor
24 Aug 2008, 9:52 PM
IMHO the mapping should only be used by the reader to map data to record fields.
The record itself should only contain fields with the specified names and not mappings.
This means that only the example should be corrected:
var myNewRecord = new TopicRecord({
title: 'Do my job please',
author: 'noobie',
totalPosts: 1,
lastPost: new Date(),
lastPoster: 'Animal',
excerpt: 'No way dude!'
});
Animal
24 Aug 2008, 10:56 PM
So then the Record constructor would have to extract the fields from the data and place them by name into the data property of the Record.
Makes sense.
It just depends at which stage the mapping->name translation takes place, at construction time, or a field access time.
Condor
24 Aug 2008, 11:04 PM
Should the constructor do a mapping->name conversion on the data? Currently it simply copies the supplied parameter to the internal data object (which is fine by me).
IMHO the mappings should actually be a config option of DataReader and not part of the Record at all.
ps. Doing a conversion at access time would really slow down data access, so that is never a good option.
Animal
25 Aug 2008, 7:26 AM
Well what does the "name" mean then? It's not used by anything.
Condor
25 Aug 2008, 7:53 AM
I think you've got your config items reversed:
Field.name is used for everything (dataIndex, displayField, valueField etc.).
Field.mapping is only used by JsonReader and XMLReader to convert data to records (this is why I think that it should have been a config item of DataReader, not of Field, but it's too late to fix that now...).
Animal
25 Aug 2008, 8:01 AM
Ah, I see!
When using a Reader the name property is used when creating the data object of the Record. The example code which started this thread does not use a Reader and so does not have that step.
I will change the example code. When directly using your Record constructor, you must use the names not the mappings.
letssurf
26 Aug 2008, 12:15 AM
Would it not make sense to have the code that does the mapping on the record contructor (the bit that is curried) when using create.
That way if anyone (like myself) who wants to use mappings direct on the data record wouldn't be a problem. Also it will be fast if not faster as you've built the actual constructor up from the data mappings and curried them over no more if mapping etc for the reader. Also logic is contained in the place where I would have thought it should be, on the record (where the data is). This would leave get and set etc untouched allowing them to stay as fast as they are.
Condor
26 Aug 2008, 1:18 AM
Currently the mapping logic is contained in JsonReader and XmlReader and not in Record.
Imagine the following record definition:
var MyRecord = Ext.data.Record.create([
{name: 'title', mapping: 'li > span[@class="title"]'},
{name: 'content', mapping: 'li > div[@class="content"]'}
]);
(which are valid mappings for an XmlReader)
What would seem more logical:
var record = new MyRecord({
'li > span[@class="title"]': 'My title',
'li > div[@class="content"]': 'Some content...'
});
or
var record = new MyRecord({
title: 'My title',
content: 'Some content...'
});
ps. If you want to add unprocessed record data to a store you should use:
store.loadData({
topic_title: 'Do my job please',
username: 'noobie',
topic_replies: 1,
post_time: new Date(),
user2: 'Animal',
post_text: 'No way dude!'
}, true);
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.