Georg Kastenhofer
Georg Kastenhofer

Reputation: 1417

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 :)

Upvotes: 0

Views: 315

Answers (2)

Neelesh
Neelesh

Reputation: 736

It is because your this is referring to this.tree instead of main object. use this -

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

});

Upvotes: 1

Richard
Richard

Reputation: 1138

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.

Upvotes: 1

Related Questions