Search code examples
sencha-touch-2

Retrieve value from Store


I have the following store:

Ext.define('Sencha.model.MenuPoint', {
extend: 'Ext.data.Model',

config: {
    fields: [
        {name: 'id', type: 'string'},
        // this is id of the element, example child id, report id, and so fourth
        //{name: 'elementId', type: 'int'},
        {name: 'name', type: 'string'},
        {name: 'icon_url', type: 'string'},
        {name: 'xtype', type: 'string'}
    ]
}
});

And I manage to insert and retrieve the value like this:

var keyValue = new Object();
keyValue.key = "adultId";
keyValue.value = adultObj.adultId;
console.log("keyValue: "+keyValue);
var sessionStore = Ext.getStore('MySessionStore');
sessionStore.add(keyValue);
sessionStore.sync();

var index = sessionStore.find('key',keyValue.key);
console.log("index: "+index);
var keyValue2 = sessionStore.getAt(index);
console.log("keyValue2: "+keyValue2);

But it so cumbersome to first get the index, then get the value, isnt it possible to do something like this:

var value = store.get(key);

?


Solution

  • you could use function

    var record = store.findRecord('id', key)
    

    it is a shortcut to

    index = store.find('id', key);
    record = index != -1 ? store.getAt(index) : null;
    

    Cheers, Oleg