Use dojo/aspect
instead:
// var aspect = require("dojo/aspect");
// var lang = require("dojo/_base/lang");
aspect.after(tree, "_onExpandoClick", lang.hitch(this, function(item) {
console.log("My onExpandoClick");
}))
Edit: Also please note, dijit/Tree
as well as cbtree/Tree
emit onOpen
and onClose
events, so you can register event listeners for expanding and collapsing node actions:
tree.on("open", function(item, node) {
console.log("onOpen:", item, node);
});
tree.on("close", function(item, node) {
console.log("onClose:", item, node);
});
Edit #2: According to the discussion below I changed my approach from using dojo/aspect
to subclassing Tree
(because of some quirky async funky there). So first subclass Tree
:
// var declare = require("dojo/_base/declare");
// var lang = require("dojo/_base/lang");
// var Deferred = require("dojo/_base/Deferred");
var MyTree = declare([Tree], {
persist: true,
_expandNode: function(node, recursive) {
var dfd = this.inherited(arguments);
Deferred.when(dfd, lang.hitch(this, function() {
if (recursive === undefined) {
this.set("state", this._objectToArray(this._openedNodes));
}
}));
return dfd;
},
_collapseNode: function(node) {
this.inherited(arguments);
this.set("state", this._objectToArray(this._openedNodes));
},
_objectToArray: function(object) {
var arr = [];
for (var each in object) {
if (object.hasOwnProperty(each)) {
arr.push(each);
}
}
return arr;
}
});
Then you can you can observe expand/collapse state change on MyTree
instance:
tree.watch("state", function(/*String*/key, /*Array*/oldValue, /*Array*/newValue) {
console.log(newValue);
});
See it in action: http://jsfiddle.net/phusick/LcPzv/