PDA

View Full Version : Question about the idProperty config on a Model



parky128
29 Nov 2011, 4:54 AM
Hi,

Im encountering some problems when working with the store and model classes when trying to configure a model with an idProperty.

I have the following model defined:


Ext.define('PinpointersTouch.model.Unit', {
extend: 'Ext.data.Model',
fields: [
{ name: 'RowNo', type: 'int' },
'UntID',
'MapIconHTML',
'UnitName',
'EventDTLocalDisplay',
'Lat',
'Lon',
'Location',
'StatusHTML',
'InJourney',
'SwitchedOn',
'HasPower',
'UsrProfilePictureGUID',
{
name: 'EventDTLocal',
type: 'date'
}
],
idProperty: 'UntID'


});


And the following store:


Ext.define('PinpointersTouch.store.Units', {
extend: 'Ext.data.ArrayStore',
requires: 'PinpointersTouch.model.Unit',
model: 'PinpointersTouch.model.Unit'


});


After loading data into the store, I have some code which loops through the items in the store and I am trying to lookup the id values for each record (model). When I inspect the id property of the model I am seeing an auto generated id and not the value of the field I have defined in the idProperty config on the model (UntID). I know I have data loaded for the record when debugging in Chrome:




data: Object

EventDTLocal: Date
EventDTLocalDisplay: "12:47:49"
HasPower: null
InJourney: true
Lat: 52.85097
Location: "M1, Lockington-Hemington, North West Leicestershire, Leicestershire heading N at 67mph. 584 RPM"
Lon: -1.29748
MapIconHTML: "<img src='/SBS.Websites.PPTouch/images/mapicons/10001.gif'>"
RowNo: 24
StatusHTML: "<img src='/SBS.Websites.PPTouch/images/icon-fuel010.gif' title='8.00% fuel'><img src='/SBS.Websites.PPTouch/images/mapicons/110.gif' title='Timed'>"
SwitchedOn: null
UnitName: "Rob AVG650"
UntID: 7393



So I'm a bit confused here, I should be seeing an id value of 7393 based on the above but clearly I have gone wrong somewhere, can anyone assist?

Thanks

mitchellsimoens
29 Nov 2011, 5:57 AM
The id property in the Model instance is a combination of the Model name and the id separated by '-'. The id property you are wanting is the internalId property.

parky128
29 Nov 2011, 6:29 AM
Thank you for your answer, however configuring the internalId property on my model has no effect when I break into my code and look at the store records, the internalId values are still the 'ext-record-x' auto generated ones.

I can only get around this by ensuring one of my fields is named 'id' and use model.getId() in my code which is not ideal. Any other ideas?

mitchellsimoens
29 Nov 2011, 5:29 PM
Then the idProperty is not getting mapped correctly in your response.

justinKohactive
29 Nov 2011, 7:31 PM
Ext.define('LegalProxy.model.Profile', { extend: 'Ext.data.Model',
hasMany: ['Locations', 'Expertises'],
idProperty: '_id',
fields: [
{name: '_id', type: 'string'},
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'},
{name: 'company', type: 'string'},
{name: 'address', type: 'string'},
{name: 'address2', type: 'string'},
{name: 'city', type: 'string'},
{name: 'state', type: 'string'},
{name: 'zipcode', type: 'string'},
{name: 'locations', type: 'string'},
{name: 'phone', type: 'string'},
{name: 'specialties', type: 'string'}
],
validations: [
{type: 'presence', field: 'firstName'},
{type: 'presence', field: 'lastName'},
{type: 'presence', field: 'address'},
{type: 'presence', field: 'city'},
{type: 'presence', field: 'state'},
{type: 'presence', field: 'zipcode'},
{type: 'presence', field: 'phone'},
{type: 'presence', field: 'specialties'},
{type: 'presence', field: 'locations'},
],
proxy: {
type: 'rest',
url: endpoint + '/profiles',
noCache: false,
reader: {
type: 'json',
getResponseData: function(response) {
try {
var data = Ext.decode(response.responseText);
}
catch (ex) {
Ext.Error.raise({
response: response,
json: response.responseText,
parseError: ex,
msg: 'Unable to parse the JSON returned by the server: ' + ex.toString()
});
}

if (!data) {
Ext.Error.raise('JSON object not found');
}

if (data['locations']) {
data.locations = data.locations.join(",");
}
if (data['specialties']) {
data.specialties = data.specialties.join(",");
}
return data;
}
},
writer: {
type: 'json',
root: 'profile',
writeAllFields: false,
getRecordData: function(record) {
var isPhantom = record.phantom === true,
writeAll = this.writeAllFields || isPhantom,
nameProperty = this.nameProperty,
fields = record.fields,
data = {},
changes,
name,
field,
key;

if (writeAll) {
console.log(fields);
fields.each(function(field){
if (field.name != '_id') {
if (field.persist) {
if (record.get('locations').length > 0) {
data['locations'] = record.get('locations').split(',');
}
if (record.get('specialties').length > 0) {
data['specialties'] = record.get('specialties').split(',');
}

name = field[nameProperty] || field.name;
if (name != 'locations' && name != 'specialties') {
data[name] = record.get(field.name);
}
}
}
});
} else {
// Only write the changes

if (record.get('locations').length > 0) {
data['locations'] = record.get('locations').split(',');
}
if (record.get('specialties').length > 0) {
data['specialties'] = record.get('specialties').split(',');
}


changes = record.getChanges();
for (key in changes) {

if (changes.hasOwnProperty(key)) {
field = fields.get(key);
name = field[nameProperty] || field.name;
if (name != 'locations' && name != 'specialties') {
data[name] = changes[key];
}
}
}
if (!isPhantom) {
// always include the id for non phantoms
data[record.idProperty] = record.getId();
}
}
return data;
}
}
},




});

justinKohactive
29 Nov 2011, 7:33 PM
Why are you extending an ArrayStore, that almost certainly wont work, i'd say use a JsonStore.