ExtJs 删除和添加侦听器。缓存问题

ExtJs removing and adding listeners. Caching issue

我有一个缓存 windows 及其所有内部结构(表单、其他 ui 元素)的对象。因此,当用户打开一个已经缓存的 window 时,它会从头开始创建它,但是如果从缓存中获取并像这样显示它:

winCache[id_group].show();

因此,对于每个 id_group,都有一个 window 可以缓存在 winCache 中。问题是我想重置 window 中的 form,并用数据库中的新数据填充它。我尝试这样做:

if(winCache[id_group]){
    winCache[id_group].removeListener('show');
    winCache[id_group].addListener('show', function () {
      populate_form(this, id_field); // <-- this method retreives data from database 
   // and sets the form  
    });
    winCache[id_group].show();  
}
else { // window was not cached and has to be created from scratch
  var win = ...
  win.show();
  // ... some BIG procedure
  winCache[id_group] = win; // next time it will be taken from cache
}

但问题是,出于某些疯狂的原因,ExtJS 无限重复 show 听众。因此,例如,如果我为相同的 id_group = 10 打开 winCache[10] 七次,但对于不同的 id_fields (id_field = 1, id_field = 2, ..., id_field = 7), 然后 show 侦听器将被触发七次 - 每个 id_field。那是难以置信的愚蠢。所以,我的问题是为什么 ExtJS 重复听众?为什么 removeListener 不起作用???

removeListener 需要函数引用作为第二个参数。

如果每次显示 window 时都执行您添加的代码,可能在 addListener 上使用 single: true 将解决您的问题(因此您不需要需要 removeListener), 尝试:

winCache[id_group].addListener('show', function () {

}, null, { single: true });

如果不是这种情况,您需要保存对要传递给 addListenershow 函数的引用,以便能够将其传递给 removeListener,或者可以使用 destroyable,例如:

winCache[id_group].destroyShowEvent && winCache[id_group].destroyShowEvent.destry();
winCache[id_group].destroyShowEvent = winCache[id_group].addListener('show', function () {

}, null, { destroyable: true });