1. #1
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    175
    Vote Rating
    19
    kostysh will become famous soon enough kostysh will become famous soon enough

      0  

    Default How to extract data from store

    How to extract data from store


    Json store with mode defined and loaded successfully.
    How to extract data from store?
    if I try to myStore.findRecord('id', 'idvalue');
    i get: Uncaught TypeError: Object [object Object] has no method 'findRecord'

    Here is sample of my code:
    Code:
    Ext.define('Myapp.controller.Super', {
        models: [        'Hero'
        ],
        
        stores: [
            'Hero'
        ],
    
        init: function() {
            var superStore = this.getHeroStore();
            superStore.on('load', function() {
                superStore.findRecord('id', 'idvalue');//here i got a error like above
            });
            superStore.load();
        }
    });

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,330
    Vote Rating
    846
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Try this:

    Code:
            superStore.on('load', function(store) {
                store.findRecord('id', 'idvalue');//here i got a error like above
            });
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    175
    Vote Rating
    19
    kostysh will become famous soon enough kostysh will become famous soon enough

      0  

    Default


    No, this not works.
    But if I use store in List or nestedList component - store works fine.

  4. #4
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    175
    Vote Rating
    19
    kostysh will become famous soon enough kostysh will become famous soon enough

      0  

    Default


    I still can't find a right way to get records from the store.
    found only one possibility: var rootNode = store.getRootNode();
    and recursively walk thru rootNode tree, and search for needed record. I think this way is wrong.


    Store object must have findRecord() method, but: Uncaught TypeError: Object [object Object] has no method 'findRecord'

    Help me please to understand how to search and records from store.

  5. #5
    Sencha User KJedi's Avatar
    Join Date
    Feb 2008
    Location
    Ukraine, Mykolayiv
    Posts
    145
    Vote Rating
    1
    KJedi is on a distinguished road

      0  

    Default


    Try checking if you're working with the store or something else:
    Code:
    superStore.on('load', function(store) {
    console.log(store.$className);
    if (store instanceof Ext.data.Store) {
                store.findRecord('id', 'idvalue');//here i got a error like above
    }
            });
    I bet something else is passed there. Put a breakpoint and go through the stack trace up to see where something wrong happens.
    I can't see a problem in the code itself.

    Does this happen with any store? What if you just try loading data into a plain store and access the records?

  6. #6
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    175
    Vote Rating
    19
    kostysh will become famous soon enough kostysh will become famous soon enough

      0  

    Default


    I checked. My superStore is instanceof Myapp.store.Superstore (extended from Ext.data.TreeStore).
    Here is definition of Myapp.store.Superstore. I think it looks good:
    Code:
    Ext.define('Myapp.store.Superstore', {
        extend: 'Ext.data.TreeStore',
        model: 'Myapp.model.Superstore',
        autoLoad: true,    
        root: {},    
        proxy: {
            type: 'ajax',
            url: 'data/data.json',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    and here is definition of model:
    Code:
    Ext.define('Myapp.model.Superstore', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'id', type: 'string'},
            {name: 'text', type: 'string'},
            {name: 'description', type: 'string'},
            {name: 'leaf', type: 'boolean',  defaultValue: false},
            {name: 'items', type: 'auto'},
            {name: 'products',  type: 'auto'}
        ]
    });
    In case of using of this store in NestedList - all works fine. Therefore store is defined correctly.

  7. #7
    Sencha User KJedi's Avatar
    Join Date
    Feb 2008
    Location
    Ukraine, Mykolayiv
    Posts
    145
    Vote Rating
    1
    KJedi is on a distinguished road

      0  

    Default


    You see, the TreeStore is a problem. It is not extended from the Ext.data.Store. And according to the API, it does not have findRecord method.
    Maybe you can explain what you're trying to achieve and we can advise how to do it correctly.

  8. #8
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    175
    Vote Rating
    19
    kostysh will become famous soon enough kostysh will become famous soon enough

      0  

    Default


    Thanks! I did not pay attention to the fact that the TreeStore has a different set of methods. I thought that if he had inherited from the normal store, then it inherits all the methods. It's my mistake.

    I trying to get a specific record from store.
    For example, my json file contains:
    Code:
    {
     "items": [
            {
                "id": 1,
                "text": "BlablaBla",
                "description": "Qwerty",
                "leaf": true,
                
                "products": [
                    {
                        "id": 11,
                        "text": "Lorem ipsum",
                        "leaf": true
                    },
                    {
                        "id": 12,
                        "text": "Dolor sit",
                        "leaf": true
                    }
                  ]
            }
        ]
    }
    Definitions of store and model are in previous post.

    How to get a record with product which has id=11?

  9. #9
    Sencha User KJedi's Avatar
    Join Date
    Feb 2008
    Location
    Ukraine, Mykolayiv
    Posts
    145
    Vote Rating
    1
    KJedi is on a distinguished road

      0  

    Default


    I understand that you're going to get the record, but why do you need it? When I asked you to describe the task, I meant to explain why do you need to get a record from this store. If you need the record on tap, maybe there is a way of getting it using the event parameters.

    Here you can try getting the root and then using findChild method

  10. #10
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    175
    Vote Rating
    19
    kostysh will become famous soon enough kostysh will become famous soon enough

      0  

    Default


    KJedi:
    I need to get records for doing some calculations. While the user works with application, I save ids of selected products from nestedlist into localstorage (like as to shopping cart). At next time for doing some calculations I need to get full products objects (related to saved ids) from the store.
    (sorry for my English, I think we can communicate by Russian because we are from Ukraine )