[英]dojo declare constructor: object member reference undefined
我不知道为什么会遇到麻烦,在下面的代码的checkAcceptance
方法中引用类成员对象this.treeStore
?
代码段( 链接到正在运行的jsfiddle示例... ):
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未定义
任何帮助将不胜感激 :)
问题在于,checkAcceptance函数内的this
关键字的作用域为dijit.Tree
小部件,而不是您自己的小部件。 只需添加一个var self = this;
在构造函数范围级别的某个位置,然后在超出范围时使用self而不是this进行引用。
这是因为您的这是指this.tree而不是主要对象。 用这个 -
dojo.declare("at.delta.util.FolderConfigurator", null, { treeStore: null, treeModel: null, tree: null, constructor: function (/*string*/ jsonData) { var _this = this; 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"); } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.