-
11 Oct 2012 2:38 PM #1
ArrayStore - idProperty
ArrayStore - idProperty
Is possible to use idProperty on model which is assign to arraystore?
Code:Ext.define("Level", { extend : "Ext.data.Model", idProperty : 'value', fields : [ { name: 'value', type: 'int' }, { name: 'text', type: 'string' } ] });Code:var levelData = []; if (condition) { levelData.push( Ext.create('Level', { value : 10 text : 'Text value' }) ); }When i check data in the store, i see ID with value: "Level-ext-record-28". Am I doing anything wrong?Code:store : Ext.create('Ext.data.ArrayStore', { model : 'Level', data : levelData }),
-
13 Oct 2012 5:53 PM #2
The id you mentioned is just the id of the model instance not record id. If you try <store>.getAt(0).getId() you will get the correct value.
The reason why you get that id is that you directly create a model instance without supplying it an id. Looking at Model constructor, you will see the syntax of creating a new model instance. It is:
Without id argument, framework will generate one.Code:constructor: function(data, id, raw, convertedData) {
When you construct an array and supply it as data to construct an array store as you did in:
the data will be parsed by reader. With each record that is already a model, it will be directly pushed into store. If the record that is not a model, reader will get record id which is based on idProperty, constructs a model instance by using the above syntax and push it into store. With these record, the model id will be Modelname + "-" + recordId which is determined basing on idProperty.Code:store : Ext.create('Ext.data.ArrayStore', { model : 'Level', data : levelData })
If you fix your code as below, everything will be OK:
Code:var levelData = []; if (condition) { // levelData.push( // Ext.create('Level', { // value : 10 // text : 'Text value' // }) // ); levelData.push({ value : 10 text : 'Text value' }); }
-
27 Dec 2012 3:14 PM #3
This didnt really fix my problem. When I check level data array, i will get array of objects:
When I check array store with correct internalId (10, 20, ...), id is still generated. When I look deep into structure and expand tree with data property, I see very bad result:Code:[Object { value = 10, text = "Level 10" }, Object { value = 20, text = "Level 20" }]
arraystore-problem.png
Am I doing anything wrong?
-
27 Dec 2012 10:49 PM #4
Ignore the id and internalId properties on the records, they don't do what you think they do. The actual id of a record is in the data and should be accessed via getId.
Quite why you're using an ArrayStore isn't clear. An ArrayStore is for reading an array of arrays, which isn't what you have. In the case where the data is passed as records this won't be a problem but the reader won't really be doing anything. The idProperty is being interpreted by the model, not the reader on the store.
Here are some working examples:
Code:Ext.define("Level", { extend : "Ext.data.Model", idProperty : 'value', fields : [ { name: 'value', type: 'int' }, { name: 'text', type: 'string' } ] }); var levelData = []; levelData.push( Ext.create('Level', { value : 10, text : 'Text value' }) ); console.log(levelData[0].getId()); var store = Ext.create('Ext.data.ArrayStore', { model : 'Level', data : levelData }); console.log(store); console.log(store.getAt(0).getId());Code:Ext.define("Level", { extend : "Ext.data.Model", idProperty : 'value', fields : [ { name: 'value', type: 'int' }, { name: 'text', type: 'string' } ] }); var levelData = []; // Format the data to be read by an Array reader levelData.push([10, 'Text value']); var store = Ext.create('Ext.data.ArrayStore', { model : 'Level', data : levelData }); console.log(store); console.log(store.getAt(0).getId());
-
28 Dec 2012 2:21 AM #5
I dont have any fixed structure. Thats why I ask in previous post, if possible create new record by model. Array of arrays is badly readable - without model on same place you dont know which value represent 'text' or 'value'. I realized my mistake yesterday when I overwrited code to simple array of arrays. Significant information is for me following part:
Ignore the id and internalId properties on the records, they don't do what you think they do. The actual id of a record is in the data and should be accessed via getId.


Reply With Quote