PDA

View Full Version : How to extract data from store



kostysh
21 Dec 2011, 6:07 PM
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:


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();
}
});

mitchellsimoens
21 Dec 2011, 7:41 PM
Try this:


superStore.on('load', function(store) {
store.findRecord('id', 'idvalue');//here i got a error like above
});

kostysh
22 Dec 2011, 4:44 PM
No, this not works.
But if I use store in List or nestedList component - store works fine.

kostysh
23 Dec 2011, 7:20 PM
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.

KJedi
24 Dec 2011, 12:18 AM
Try checking if you're working with the store or something else:

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?

kostysh
24 Dec 2011, 6:32 AM
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:


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:


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.

KJedi
24 Dec 2011, 6:45 AM
You see, the TreeStore is a problem. It is not extended from the Ext.data.Store. And according to the API (http://docs.sencha.com/touch/2-0/#!/api/Ext.data.TreeStore), 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.

kostysh
24 Dec 2011, 7:05 AM
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:


{
"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?

KJedi
24 Dec 2011, 9:24 AM
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 (http://docs.sencha.com/touch/2-0/#!/api/Ext.data.NodeInterface-method-findChild) method

kostysh
24 Dec 2011, 11:09 AM
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 :)))

KJedi
26 Dec 2011, 1:40 PM
The idea was to get the data not by querying the store, but as a parameters passed to event handlers. But I'm not sure what is passed there.

PM me for communicating in Russian or joing the Russian google group for ExtJS: ruextjs@googlegroups.com

monta
11 Feb 2012, 5:46 AM
Hi evrybody,
I have the same problem as KJedi, i
need to get records from a Ext.data.JsonPStore
for doing some calculations,
i tried the getById() method but that didn't work and it show me the same error as KJedi "
Uncaught TypeError: Object [object Object] has no method ..."

help please!

Cheers
Monta,