1

我正在尝试将环绕方面用于expandaGridX的树的功能。

简化的代码如下:

var myGrid = ...; // features a tree
var myConditional = ...; // some boolean


dojo.aspect.around(myGrid.tree, "expand", function(original) {
    return function(id, skip) {
        // TODO check the conditional and decide whether 
        // to return the deferred function as is, or 
        // return a function that does nothing but log into the console
        var deferred = original(id, skip);
        return deferred;
    };
});

不幸的是,按原样调用dojo 方面(即没有对我的条件等进行任何检查)是有问题的。

单击 expando 后,控制台中将引发错误:

Uncaught TypeError: t.isExpanded is not a function

...指向 GridX 树模块的expand原始函数的主体:

var d = new Deferred(), t = this;
if(!t.isExpanded(id) && t.canExpand(id)){ // here

显然,我对 aspect around 如何工作的解释是错误的,并且范围t成为Window对象而不是树本身。

我希望有一个我可以使用的快速修复/解决方法?

澄清我的实际目的

在某些情况下,网格底层存储所查询的后端会在短时间内无法访问。事物的实现方式,扩展树的节点将查询后端。在后端不可用的非常短的窗口期间(我可以从前端代码中轻松知道),我想忽略对扩展的点击)。

4

2 回答 2

2

尝试将树实例绑定到您的函数。像这样的东西:

var myGrid = ...; // features a tree
var myConditional = ...; // some boolean

const condExpand = function(id, skip) {
        var deferred = original(id, skip);
        return deferred;
    }.bind(myGrid )


dojo.aspect.around(myGrid.tree, "expand", function(original) {
    return condExpand
});

我不确定在您的情况下特定上下文在哪里丢失,但是您可以使用它来使其适合您。

更新:

试图重现情况。下面是工作示例:

    const testObject = {    
      isNumeric: function(number) {
        return typeof number === "number"
      },
      testMethod: function() {
        console.log('testMethod', this)
        console.log("Is 5 a number: ", this.isNumeric(5))
      }
    }

    const aroundFunc = function(originalTestmethod){
        return function(method, args){
          // doing something before the original call
          console.log("Before", this)

          //Here context still corect. 
          //So bind it to passed here original method:

          var deferred = originalTestmethod.bind(this)(method, args)


          // doing something after the original call
          console.log("After")
          return deferred;

        }
      }

require(['dojo/aspect'], function(aspect) {
   aspect.around(testObject, "testMethod", aroundFunc)  
   testObject.testMethod()  

})

JS小提琴

于 2018-04-19T17:13:02.353 回答
0

我找到了一个看起来绝对野蛮但对我有用的“解决方案”。

希望有人有更好的主意,可能使用 dojo/aspect。

我已经将Tree.js模块的代码复制粘贴到我自己的实现中,只添加了我自己的条件:

myGrid.tree.expand = function(id, skipUpdateBody)  {
    var d = new dojo.Deferred(),
        t = this;
    // added my condition here
    if ((!t.isExpanded(id) && t.canExpand(id)) && (myCondition)) {
        // below code all original
        t._beginLoading(id);
        t.grid.view.logicExpand(id).then(function(){
            dojo.Deferred.when(t._updateBody(id, skipUpdateBody, true), function(){
                t._endLoading(id);
                d.callback();
                t.onExpand(id);
            });
        });
    }else{
        d.callback();
    }
    return d;
};
于 2018-04-19T13:19:30.173 回答