PDA

View Full Version : How to insert a new record to a store



Speedy
18 Aug 2011, 1:47 AM
Hi there,

i defined a store



Ext.define( 'App.store.general.MyStore' , {
extend: 'Ext.data.Store',

autoLoad: true, fields: ['display', 'value'], name: 'myStore', storeId: 'myStore',
proxy: {
type: 'ajax', url:'myUrl',
extraParams: { ... },
reader: {
type: 'json', root: 'data', idProperty: 'value'
}
}
});


in my Application I create this store via



var store = Ext.create( 'App.store.general.MyStore', {} );


so far everything works fine, the store is loaded and used within a combobox.
now I want to add a new record to the store and I tried


store.add({ display: 'abc', value: '0' });


and



store.insert( 0, { display: 'abc', value: '0' });



since the data of the loaded store start their values from 1-15 I thought 0 or 16 should work but there simple happens nothing. No error, no new entry.

What am I doing wrong?


Best

Speedy

tobiu
18 Aug 2011, 1:50 AM
store.insert(0, new Ext.data.Model({ display: 'abc', value: '0' }));


best regards
tobiu

Speedy
18 Aug 2011, 1:58 AM
thx for the try but if I try this firefox tells me "me.fields is undefined" in Model.js(606)

I guess this is because I dont use a Model.
Still I thought the reader should have enough information to convert the given object properties and create a record.

Even more weird is, that the insert works without any problem if I dont use autoLoad and use a memoryProxy, load the data via Ajax call and use the loadData() method.

skirtle
18 Aug 2011, 4:55 PM
If you specify fields instead of a model then the store will create an implicit model. This shouldn't be a problem.

When you call add() or insert() it is expecting to be passed records. However, from a quick look at the code, it is clear that if you don't pass it records it will create a record for you from the model (in your case, the implicit model).

I tried your code for myself and as far as I could tell it works fine: add() and insert() both do exactly what you'd expect.

However...

If the data is loaded via Ajax then you have to wait until after the data is loaded before adding the new record. When the Ajax request completes it will blow away any existing records before adding its new records.

I also briefly had a problem using the store with a combobox. I forgot to include queryMode: 'local', which resulted in a second Ajax request being made that blew away my new record. This is easily diagnosed, just watch the network activity in Firebug or the Chrome Developer Tools to check it isn't performing an extra request.

Speedy
19 Aug 2011, 4:19 AM
queryMode was set to local but the async loading of the ajax call was the problem

I defined a listener for the load event and added the record in there, now everything is working,

thx a lot for your help


Speedy