简体   繁体   English

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

[英]Extjs - Best way to iterate through displayed records in a buffered store

So, I'm upgrading from EXT 4.1.1a to 4.2.2 and have come across a problem with buffered stores. 所以,我正在从EXT 4.1.1a升级到4.2.2并且遇到了缓冲存储的问题。 In 4.1.1a I could use store.each to iterate through the currently displayed store records, but in 4.2.2 I simply get the error: 在4.1.1a中我可以使用store.each迭代当前显示的商店记录,但在4.2.2中我只是得到错误:

TypeError: Cannot read property 'length' of undefined

Basically inside the store object the data property does not have an items property anymore, and the each method uses the length property of the items , hence the error. 基本上在store对象中, data属性不再具有items属性,并且each方法都使用itemslength属性,因此出错。 Instead the items in the store seem to reside in data.map . 而是商店中的项目似乎驻留在data.map I could loop through data.map but it seems there should be a better way. 我可以遍历data.map但似乎应该有更好的方法。 The docs only mention store.each as the way to do this even though this seems to fail for buffered stores. 文档只提到store.each作为执行此操作的方法,即使这对缓冲商店来说似乎失败了。

I'm iterating through the store on the refresh listener attached to the grids view. 我在连接到网格视图的refresh侦听器上遍历商店。

Any help with this would be much appreciated 任何有关这方面的帮助将非常感激

Apparently they think you can't iterate over the store because it has "sparse" data, but that is not true. 显然他们认为你不能遍历商店,因为它有“稀疏”的数据,但事实并非如此。 Currently, what you could do is the following. 目前,您可以做的是以下内容。

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);
}

IMPORTANT 重要

Take care that can change the structure of the store in the future which will surely brake your code. 请注意,将来可能会改变商店的结构,这肯定会破坏您的代码。

Additionally, I strongly believe that if proper design patterns were used, each had to take care of the looping without caring about the structure. 另外,我坚信如果使用适当的设计模式, each都必须在不关心结构的情况下处理循环。

OPTIMIZATION 优化

What I usually do, when I initialize the store is the following: 我通常做的,当我初始化商店时如下:

if(store.buffered) {
    store.iterate = store.data.forEach;
} else {
    store.iterate = store.each;
}

Then you could just use it like this: 然后你可以像这样使用它:

store.iterate(fn, scope);

This is not the best decision but simplifies writing a lot of if -statements 这不是最好的决定,但简化了写很多if- statement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM