javascriptdojodijit.tree

dojo declare constructor: object member reference undefined


I have no idea why I get into troubles, referencing the class member object this.treeStore in the checkAcceptance method of the following piece of code?

Code Snippet (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");
  }

});

Attempting to drag a node of the tree results in the following error on the firefox/firebug console:

TypeError: this.treeStore is undefined

Any help will be greatly appreciated :)


Solution

  • The problem is that the this keyword inside the checkAcceptance function is scoped for the dijit.Tree widget and not your own widget. Just add a var self = this; somewhere at the constructor scope level and then use self instead of this to reference when you're out of scope.