0

I maintain a custom library consisting of many dijit widgets at the company I work at.

Many of the defects/bugs I have had to deal with were the result of this.inherited(arguments) calls missing from overriden methods such as destroy startup and postCreate.

Some of these go unnoticed easily and are not always discovered until much later.

I suspect I can use dojo\aspect.after to hook onto the 'base' implementation, but I am not sure how to acquire a handle to the _widgetBase method itself.

Merely using .after on the method of my own widget would be pointless, since that wouldn't check whether this.inherited(..) was inded called.

How can I write a generic test function that can be passed any dijit/_WidgetBase instance and checks whether the _widgetBase's methods mentioned above are called from the widget when the same method is called on the subclassing widget itself?

Bottom-line is how do I acquire a reference to the base-implementation of the functions mentioned above?

4

2 回答 2

0

在阅读了 dojo 的文档、declare.js 代码、调试、谷歌搜索、调试和黑客攻击之后,我最终得到了这段代码来获取最后一个继承的类/混合的基本方法的句柄,但我并不完全满意与调用 getInherited 相关的 hackiness:

编辑 2我用一个空数组替换了 getInherited 的第二个参数。虽然我实际上得到了对基类方法的引用,但 usingaspect不起作用。看来这种方法是失败的。

require(['dijit/registry','dojo/_base/declare','mycompany/widgets/widgetToTest'],
function(registry,declare,widgetToTest)
    {

      var widget = registry.byId('widgetToTestId');
      var baseStartup = getBaseMethod(widget,'startup');

      function getBaseMethod(widget,methodName){
        return widget.getInherited(methodName,[]);
      }

      //This is the method body I want to use .after on to see if it was called, it returns the last overriden class in the array of inherited classes. (a mixin in this case, good enough for me!)
      alert(baseStartup);

    });
于 2014-09-24T07:28:23.973 回答
0

我已经放弃尝试使用dojo/aspect.

相反,我选择修改我们的自定义基本小部件的代码以合并如下片段。在创建控制台调用及其内容被删除的发布版本时,它们会被自动删除:

console.log( 
        function(){
            (this._debugInfo = this._debugInfo|| {}).postCreate=true;
        }.call(this)  
    );

我在单元测试附近添加的样板代码中的一个简单方法是可用的,这样我就可以mycompany.widgets.basewidget在它们各自的单元测试中的所有实例上调用它。

于 2014-09-24T12:22:57.063 回答