-2

I have an array with 1 or more records. for every record for ex: there is a property id. I want to set this name to a default value : 8. Please help me. Array: [{id: 2, name: "x"},{id: 4, name : "y"}] I tried this way

if (!Ext.isEmpty(detailLineItems) && (detailLineItems.length > 0)) {
canProcess = false;
Ext.Array.forEach(array, function (r) {
    if (r.id == 0 || Ext.isEmpty(r.id)) {
        canProcess = true;
       //how do I set this id
        r.id == 6;

    }
});
Sweety
  • 31
  • 6

2 Answers2

2

You should not change the internal id of records, as this may break everything; the value in record.id is expected by the framework to be the same value as record.get(record.idProperty). Instead, you should always use the setter method: record.set("id", 8).

But what you are essentially searching for is the defaultValue configuration on your model field:

fields:[{
    name: 'id',
    type: 'int',
    defaultValue: 8
}]

Please be advised that the field id gets a special handling in ExtJS, as long as the model's idProperty defaults to id. A store can contain only one record with the same non-null value in the field defined by idProperty, so if you add them all to the same store you end up with only one of the records, and all other ones are deleted. So if you need multiple records with the same id in the store, you have to change the idProperty on the model to something else, e.g.

idProperty: 'someNonExistentProperty'

This then may cause issues with store sync operations. If you only try to set the id to a fixed value because your backend requires integer id, and the default id generated for new records by ExtJS is non-numeric (e.g. ext-myModel-1), you can check out identifier: 'negative'.

Alexander
  • 19,906
  • 19
  • 75
  • 162
0

Your question is already answered by Alexander but just to mention, your code statement r.id == 6; is using the equality operator rather than the assignment operator r.id = 6; :-)

Amita Suri
  • 309
  • 1
  • 7