2

在我的控制器中,我想从本地存储中获取随机记录并显示在视图中,我该怎么做?

在我的配置中,我有:

stores: ['Items']

在我的功能中,我有:

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

但它卡在第一行

也许有更简单的方法?

4

3 回答 3

1
 mystore.totalCount()

should be

 mystore.getCount()
于 2012-05-22T13:49:15.387 回答
0

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

于 2012-05-22T20:59:54.687 回答
0

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.

于 2012-05-22T00:31:52.237 回答