dojo 声明构造函数:对象成员引用未定义
dojo declare constructor: object member reference undefined
我不知道为什么会遇到麻烦,在下面一段代码的checkAcceptance
方法中引用了class成员对象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 进行引用。
因为你的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");
}
});
我不知道为什么会遇到麻烦,在下面一段代码的checkAcceptance
方法中引用了class成员对象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 进行引用。
因为你的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"); } });