Threaded View
-
18 Aug 2011 1:47 AM #1
Answered: How to insert a new record to a store
Answered: How to insert a new record to a store
Hi there,
i defined a store
in my Application I create this store viaCode: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' } } });
so far everything works fine, the store is loaded and used within a combobox.Code:var store = Ext.create( 'App.store.general.MyStore', {} );
now I want to add a new record to the store and I tried
andCode:store.add({ display: 'abc', value: '0' });
Code: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
-
Best Answer Posted by skirtle
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.


Reply With Quote