1. #1
    Sencha User
    Join Date
    Jun 2009
    Posts
    410
    Vote Rating
    2
    parky128 is on a distinguished road

      0  

    Default Question about the idProperty config on a Model

    Question about the idProperty config on a Model


    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:

    Code:
    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:

    Code:
    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:

    Code:
    • 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

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,327
    Vote Rating
    851
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    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.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Jun 2009
    Posts
    410
    Vote Rating
    2
    parky128 is on a distinguished road

      0  

    Default


    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?

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,327
    Vote Rating
    851
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Then the idProperty is not getting mapped correctly in your response.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  5. #5
    Sencha User
    Join Date
    Oct 2011
    Posts
    55
    Vote Rating
    0
    justinKohactive is on a distinguished road

      0  

    Default Works for me

    Works for me


    Code:
    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;
              	 }
              }
        },
    
    
    
    
    });

  6. #6
    Sencha User
    Join Date
    Oct 2011
    Posts
    55
    Vote Rating
    0
    justinKohactive is on a distinguished road

      0  

    Default Your problem is...

    Your problem is...


    Why are you extending an ArrayStore, that almost certainly wont work, i'd say use a JsonStore.