Skip to content Skip to sidebar Skip to footer

Calling Object Methods Internally In Dojo

I am trying to create a dojo class which contains functions which in turn call other functions within this class, as follows: dojo.provide('my.drawing'); dojo.declare('my.drawing',

Solution 1:

addPoint() is not called correctly, not in the right context. Judging by its signature my psychic abilities tell me that you use it as an event handler, but you don't do it correctly.

You do it like that:

var myDrawing = new my.drawing(someArgs);

// this is incorrect:
dojo.connect(someDomNode, "onclick", myDrawing.addPoint);

// or, less incorrect, yet incorrect too:
surface.connect("onclick", myDrawing.addPoint);

In JavaScript the above lines pass a function, not a bound method as you would expect. You need to pass a context (an object to call a function on):

// the node connection:
dojo.connect(someDomNode, "onclick", myDrawing, "addPoint");
// or:
dojo.connect(someDomNode, "onclick", myDrawing, myDrawing.addPoint);

// the surface connection:
surface.connect("onclick", myDrawing, "addPoint");
// or:
surface.connect("onclick", myDrawing, myDrawing.addPoint);

Alternatively you can always use dojo.hitch() to bound the context/scope using a closure (which is done by examples above:

var boundMethod = dojo.hitch(myDrawing, "addPoint");
// or://var boundMethod = dojo.hitch(myDrawing, myDrawing.addPoint);// and now you can do like you did originally:
dojo.connect(someDomNode, boundMethod);

// or:
surface.connect("onclick", boundMethod);

Read all about it in the documentation:

Post a Comment for "Calling Object Methods Internally In Dojo"