Extjs - 迭代缓冲商店中显示的记录的最佳方式

Coi*_*_op 4 javascript extjs

所以,我正在从EXT 4.1.1a升级到4.2.2并且遇到了缓冲存储的问题.在4.1.1a中,我可以使用store.each迭代当前显示的商店记录,但在4.2.2中,我只是得到错误:

TypeError: Cannot read property 'length' of undefined
Run Code Online (Sandbox Code Playgroud)

基本上在商店对象内部,属性不再data具有items属性,并且该each方法使用了length属性items,因此错误.相反,商店中的商品似乎存在于data.map.我可以循环data.map但似乎应该有更好的方法.docs只提到store.each作为执行此操作的方法,即使这对于缓冲商店来说似乎失败了.

我在refresh连接到网格视图的侦听器上遍历商店.

任何有关这方面的帮助将非常感激

ziG*_*iGi 7

显然他们认为你不能遍历商店,因为它有"稀疏"的数据,但事实并非如此.目前,您可以做的是以下内容.

if(store.buffered) {
    // forEach is private and part of the private PageMap
    store.data.forEach(function(record, recordIdx) {
        /* Do stuff with the record here */
    }, this);
} else {
    store.each(function(record) {
        /* Do the same stuff I guess */
    }, this);
}
Run Code Online (Sandbox Code Playgroud)

重要

请注意,将来可能会改变商店的结构,这肯定会破坏您的代码.

此外,我坚信如果使用适当的设计模式,each必须在不关心结构的情况下处理循环.

优化

我通常做的,当我初始化商店时如下:

if(store.buffered) {
    store.iterate = store.data.forEach;
} else {
    store.iterate = store.each;
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

store.iterate(fn, scope);

这不是最好的决定,但简化了写很多if- statement