1. #1
    Sencha User
    Join Date
    Aug 2011
    Posts
    50
    Vote Rating
    16
    stevo.zilik will become famous soon enough

      0  

    Default Unanswered: store.getById too slow

    Unanswered: store.getById too slow


    Hi,

    I need a faster search mechanism than iterating through all the records in the store by using store.getById(...).

    I've looked at the code and tried to replace it with
    Code:
    Ext.override(Ext.data.Store, {
        getById: function (id) {
            return (this.snapshot || this.data).getByKey(id);
        }
    });
    but the records are not mapped using the Model configured 'idProperty', instead it's mapped using some auto-generated 'internalId' which is not something I can utilize.

    What can I do to search the store by id using a map?

    Thanks,
    Stevo

  2. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,154
    Vote Rating
    475
    Answers
    685
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      0  

    Default


    Is there no unique id field in the table?

    Can you use a for/loop for all the records and perform your action instead of do a hard id search for each?

  3. #3
    Sencha User
    Join Date
    Aug 2011
    Posts
    50
    Vote Rating
    16
    stevo.zilik will become famous soon enough

      0  

    Default


    I have unique id's, which I'd love to use, but the store doesn't let me, there is no api for it

    I need to find one specific record from the store by it's id (idProperty). I don't want to use for loop, that's expensive, I want to use (some kind of) map

  4. #4
    Sencha Premium Member krullj's Avatar
    Join Date
    Sep 2011
    Posts
    35
    Vote Rating
    7
    Answers
    2
    krullj is on a distinguished road

      0  

    Default


    How many records are in the store that you're using getById() on? I'm wondering why it's being slow for you. If you really want to map the records, you could do it when the store loads (or some other event happens). Here is sample code for writing a map and overriding the default getById function to use it:
    Code:
    Ext.define('App.override.Store', {
      override: 'Ext.data.Store',
      getById: function (id) {
        return this.idMap[id];
      }
    });
    
    var store = Ext.create('Ext.data.Store', {
      storeId: 'names',
      fields: ['id','Name'],
      data: [
        { id: 1, Name: 'Bob' },
        { id: 2, Name: 'Joe' },
        { id: 3, Name: 'Watson' },
      ]
    });
    
    store.on('load', function(store, records, successful) {
    
      store.idMap = {};
    
      if (successful) {
        Ext.Array.forEach(records, function(record) {
          store.idMap[record.getId()] = record;
        });
      }
    });
    Remember, you could always name the new method something other than getById.

Thread Participants: 2

Tags for this Thread