Hybrid View

  1. #1
    Sencha User
    Join Date
    Mar 2009
    Posts
    84
    Answers
    1
    Vote Rating
    0
    Speedy is on a distinguished road

      0  

    Default 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

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

    Code:
    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
    Code:
    store.add({ display: 'abc', value: '0' });
    and

    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

  2. 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.

  3. #2
    Sencha User tobiu's Avatar
    Join Date
    May 2007
    Location
    Munich (Germany)
    Posts
    2,684
    Answers
    130
    Vote Rating
    112
    tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all

      0  

    Default


    Code:
    store.insert(0, new Ext.data.Model({ display: 'abc', value: '0' }));

    best regards
    tobiu
    Best regards
    Tobias Uhlig
    __________

    S-CIRCLES Social Network Engine

  4. #3
    Sencha User
    Join Date
    Mar 2009
    Posts
    84
    Answers
    1
    Vote Rating
    0
    Speedy is on a distinguished road

      0  

    Default


    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.

  5. #4
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,603
    Answers
    543
    Vote Rating
    325
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    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.

  6. #5
    Sencha User
    Join Date
    Mar 2009
    Posts
    84
    Answers
    1
    Vote Rating
    0
    Speedy is on a distinguished road

      0  

    Default


    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

Thread Participants: 2