Laura James
Laura James

Reputation: 35

Sencha touch 2 filterby() not updating records

I have a nested list on one of the pages of a Tabbed Panel app that is pulling data from "offices.json"

I should like to be able to filter this list when a user clicks on a toolbar button. However my filterBy() function doesn't update the store and the list of offices I can see, even though I can see in the console it is iterating the records and finding a match. What am I doing wrong? (And yes I have tried doing s.load() both before and after the filterBy to no avail!)

toolbar:{                        
   items:[{
           text: 'Near you',
           id: 'btnNearYou',
           xtype: 'button',
           handler: function() {
             s = Ext.StoreMgr.get('offices');
             s._proxy._url = 'officesFLAT.json';        
             console.log("trying to filter");
             s.filterBy(function(record) {
              var search = new RegExp("Altrincham", 'i'); 
              if(record.get('text').match(search)){
               console.log("did Match");
               return true;
              }else {
               console.log("didnt Match");
               return false;
             }
           });
           s.load();
          }                            
   }]

For the record I'm defining my store like so:

store: {
    type: 'tree',
    model: 'ListItem',
    id: 'offices',
    defaultRootProperty: 'items',
    proxy: {
        type: 'ajax',
        root: {},
        url: 'offices.json',
        reader: {
            type: 'json',
            rootProperty: 'items'
        }
    }
}

Upvotes: 2

Views: 877

Answers (1)

Evan Trimboli
Evan Trimboli

Reputation: 30082

  1. No need to recreate the regex each time, cache it outside.

  2. You can simplify the code a lot (see below).

  3. Why are you calling load directly after? That's going to send it to the server and it will just retrieve the same dataset.

toolbar: {
    items: [{
        text: 'Near you',
        id: 'btnNearYou',
        xtype: 'button',
        handler: function() {
            s = Ext.StoreMgr.get('offices');
            s._proxy._url = 'officesFLAT.json';
            var search = /Altrincham/i;
            s.filterBy(function(record) {
                return !!record.get('text').match(search);
            });
        }
    }]
}

Upvotes: 2

Related Questions