extjsextjs4extjs-stores

How to access the return value from a store.filter in extjs?


I have the following code, where Im creating a store and querying it on load by passing the name_holder value. So bascially the query would be

 /query/name:name_holder* 

where the name_holder value is entered by the user. Now im trying to retrieve the returned record value but I'm not able to access it outside the store.load function. Is my approach even correct? Im new to ext.js and any help would be appreciated. Thank you.

var store = Ext.create("Ext.data.Store", {
                    fields: [
                             {name: 'name', type: 'string'},
                             {name: 'id',  type: 'string'},

                         ],
                    proxy : name_proxy,
                });

var data_names= store.load({
                    params : {
                        query : name_holder
                    },
   callback: function(records, operation, success) {
                            if (success) {
                                var data_id=  records[0].data.id;
                             } else {
                                console.log('error');
                            }
                        }
                });
    console.log(data_id);

I can access the data_id value inside the function, but not outside. Hence the console.log line gives me an error.


Solution

  • If the actual task is to find out the id of the record whose name exactly matches a given string, then you don't need the whole fuss with stores. Just get your server side API return the ID in response to query like /query/name:name_holder. Simply implement an AJAX request to get the ID.

    If you need to stick with stores for some reason, then the best would be, again, get the server side to perform exact match so that it returns only one record when /query/name:name_holder is requested. In this case your store will have only one record upon loading, and you'll easy find its id this way: store.getAt(0).getId().

    Finally, if you can't or don't want to touch anything on the server side, use the query method to filter out the record of your interest:

    store.query(
             'name',
             name_holder,
             false, // not anymatch
             true, // case sensitive
             true // exact match
         ).getAt(0).getId();
    

    Example with local data: https://fiddle.sencha.com/#fiddle/pbo