James Gu
James Gu

Reputation: 1392

Sencha Touch 2 get record

In my controller, I want to get a random record from the localstorage and display in the view, how can I do that?

In my config I have:

stores: ['Items']

In my function I have :

var mystore = (this.getStores())[0];  
var index = this.getRandomInt(0, mystore.totalCount() -1);
var nextItem = mystore.getAt(index);

but it's stuck at the first line

Maybe there is an easier way?

Upvotes: 2

Views: 3761

Answers (3)

Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

Here's the function you shoud use

var getRandomRecord = function(store){
  var s = Ext.getStore('store');
  var r = s.getAt(Math.floor(Math.random()*(s.getCount()-1)));
  return r;
}

And you call it like this :

var randomRecord = getRandomRecord('Items');

Hope this helps

Upvotes: 0

Naresh Tank
Naresh Tank

Reputation: 1568

 mystore.totalCount()

should be

 mystore.getCount()

Upvotes: 1

Adam Marshall
Adam Marshall

Reputation: 6055

Since your store is called 'Items' i would:

var mystore = Ext.getStore('Items');

is the most reliable way to get the instance of the store.

Upvotes: 0

Related Questions