javascript - dojo声明构造函数: object member reference undefined

标签 javascript dojo dijit.tree

我不知道为什么会遇到麻烦,在下面代码的 checkAcceptance 方法中引用类成员对象 this.treeStore

代码片段(link to a running jsfiddle example...):

dojo.declare("at.delta.util.FolderConfigurator", null, {
    treeStore: null,
    treeModel: null,
    tree: null,

    constructor: function (/*string*/ jsonData) {
        this.treeStore = new dojo.data.ItemFileWriteStore({
        data: jsonData
        });
        this.treeModel = new dijit.tree.ForestStoreModel({
           store: this.treeStore // this.treeStore available, perfect no problems
        });  
        this.tree = new dijit.Tree({
           model: this.treeModel,
           showRoot: false,
           betweenThreshold: 5,
           dndController: "dijit.tree.dndSource",
           checkAcceptance: function ( /*Object*/ source, /*Array*/ nodes) {
               // Here comes the code for drag acceptance
               var currentNode = dijit.getEnclosingWidget(nodes[0]).item;
               var parentNode = this.treeStore.getValue(currentNode, 'parent'); // typeError: this.treeStore is undefined
               return (parentNode && parentNode.owners !== undefined);
        }
    }, "pnlTree");
  }

});

尝试拖动树的节点会导致 firefox/firebug 控制台上出现以下错误:

TypeError: this.treeStore is undefined

任何帮助将不胜感激:)

最佳答案

问题是 checkAcceptance 函数中的 this 关键字的作用域是 dijit.Tree 小部件,而不是您自己的小部件。只需在构造函数作用域级别的某个位置添加一个 var self = this; ,然后在超出作用域时使用 self 而不是 this 进行引用。

关于javascript - dojo声明构造函数: object member reference undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34028345/

相关文章:

xpages - djextNameTextBox(Dojo 名称文本框)控件的样式需求和示例

javascript - Dojo DataGrid (DGrid) 添加复选框列

javascript - Dojo 1.10 - 刷新 TreeView ,同时保持展开的节点状态

javascript - Jquery animate backgroundColor 不工作

javascript - jQuery - 如何调整 CSS 过滤器(模糊)?

javascript - 使用“确定”和“取消”创建消息框 asp.net

c# - 创建谷歌地图网站 - 将城市映射到纬度和经度

javascript - TheIntern.IO 测试运行程序实习生客户端问题 "Attempt to require unloaded module superagent"

dojo - 使用'before'选项将商品添加到dojo商店

javascript - 展开 dojo 树中的特定节点