我不知道为什么会遇到麻烦,在下面代码的 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/