PDA

View Full Version : Collect from Store



1chiban
5 Dec 2011, 7:07 AM
Hi guys,

I need to populate a combobox with distinct values from a store. So I found this method, collect which should return unique values from a provided data index inside a store. But it looks like it's not returning anything when i try to use it.


var AddressStore = Ext.create('Ext.app.Stores.Addresses');
console.log(AddressStore.collect('district'));
var DistrictStore = Ext.create('Ext.data.SimpleStore', {
fields:[{name: 'district', type: 'string'}],
data: EnderecoStore.collect('district')


When i click on the combobox there are no values, it's all blank. I guess I could do it on server side but it would give much more work. The attribute i want it's just a field in my object. So when i try to send the JSON data from the server, it gives me an array like this {"DistrictList": ["District1","District2","District3","District4","District5","District6","District7","District8","District9","District10","District11","District12","District13","District14","District15","District16","District17","District18"]}.

Using this approach i had to create a store in a different way like :


Ext.define('Ext.app.Stores.Districts', {
extend: 'Ext.data.Store',
fields:[
{name:'district', type:'string'}
],
proxy: {
type: 'ajax',
url : '/App/Addresses/Districts',
reader: {
type: 'json',
root: 'DistrictList'
}
},
autoLoad: true
});

Using the collect method it would make it much easier but i haven't managed to make it work yet 8-|

mitchellsimoens
5 Dec 2011, 8:12 AM
There is no SimpleStore. Doing the collect on a Store, do you get the data you want?

1chiban
5 Dec 2011, 9:31 AM
Ah right i had a Ext.data.Store previously. And it doesn't return anything, just an empty array on the console [] :(

mitchellsimoens
5 Dec 2011, 9:32 AM
You're doing the collect after the store has been loaded too correct?

1chiban
5 Dec 2011, 9:55 AM
Yup the store has the autoload on its definition. I even tried to check that doing:


console.log(AddressStore);
console.log(AddressStore.collect('district'));


On the console it shows me the store object with about 300 items and then on the second command just gives me a '[]'

ldonofrio
5 Dec 2011, 10:03 AM
do the collect in a load listener

mitchellsimoens
5 Dec 2011, 10:56 AM
Yup the store has the autoload on its definition. I even tried to check that doing:


console.log(AddressStore);
console.log(AddressStore.collect('district'));


On the console it shows me the store object with about 300 items and then on the second command just gives me a '[]'

Yes but your store still isn't loaded. Load will happen in the background and since the Store is really just a Javascript Object, the Object you console.log out will be updated when the data is updated. console.log(AddressStore.getCount()); and see if it is loaded.

I am 99.9% sure your store isn't actually loaded when you try to do collect()

ldonofrio
5 Dec 2011, 11:02 AM
that's right, never trust in objects and arrays logged by firebug

1chiban
6 Dec 2011, 1:55 AM
:) thats right it returns a 0 when i do the getCount(). so should i make the collect inside a load listener? I tried to use a getCount directly through the console and now it shows the correct value, 338 and it works when i try to collect the store. Just when i try to use the AddressStore.collect() as the store for my combobox,

anchor: '100%', fieldLabel: 'District',
id:'distcombo',
name: 'district',
store: AddressStore.collect('district'),
displayField: 'district'

and then try to do on the console Ext.getCmp('distcombo').getStore().getCount() it returns 0. So it looks like the result of the collect it can't be put on a store no?

Edit: I managed to load the data on the combobox store but now although i can see the store has 2 items, they aren't on the correct format i guess: ["District1", "District2"]. When I retrieve the store through the console, i can search the store and get data > items > data> District1: undefined, instead of district: District1