PDA

View Full Version : Small data store question



dachr
3 Mar 2011, 7:14 AM
Hey
I'm quite new to Sencha Touch, and I'm trying to learn how the data store is working.
I got a small issue that I'm sure is pretty easy to solve, I just can't figure out just the way, so I was hoping someone in here could give me a nudge in the right direction :)

Right now I have a php file outputting json that looks like this:


{
"totalCount": 30,
"items": [
{
"id": "728",
"pic": "picture.jpg",
"header": "Some header text",
"time": "Some date",
"content": "Some content text"
}
]
}

And my data is loaded into the site like this:


var ListStore = new Ext.data.Store({
model: 'Items',
sorters: [
{
property : 'id',
direction: 'DESC'
}],
proxy: {
type: 'ajax',
url : 'resources/includes/inc_json.php',
reader: {
type: 'json',
root: 'items'
}
},
autoLoad: true
});

I got two places on my site where I want to use the data from this store. One is a list that shows all 30 items which is working perfectly fine.
The other part is a caroussel in which I want to show the 5 first elements from the json store.
From what I can gather I need to use the getRange method - Something along the lines of
ListStore.getRange(0,4)

My main issue is that I'm also using the "each" method of the store - And apparently I can't type something like:


var counter = 0;
ListStore.getRange(0,4).each(function() {
console.log(counter);
counter++;
})

I tried to do something like this instead:


var counter = 0;
newStore = ListStore.getRange(0,4);
newStore.each(function() {
console.log(counter);
counter++;
})

But since newStore is only a normal array when I do it like that, the "each" method doesn't work.

As mentioned in the beginning, this is a pretty small issue, and I'm sure it's quite easy to do.. I just can't see it :">

Thanks,
Daniel

ngardner
3 Mar 2011, 8:54 AM
try...



var counter = 0;
newStore = store.getRange(0,4);
for(counter in newStore) {
console.log(newStore[counter]);
}

dachr
3 Mar 2011, 3:07 PM
Thanks for the answer :)

Ended up just making a counter inside the function that loads the json into the carousel, and told it to stop the function when the counter reached 5.

It was a somewhat easier solution though not as neat as using the getRange method - It does the job, and keeps me from getting more confused for now :P