Saturday, March 8, 2014

Add and drop event listeners in dojox.

The postCreate function which is empty here ended up looking like this:

postCreate: function() {
   window.addEventListener('resize', lang.hitch(this, this.prepChart));
},

 
 

The afterActivate method in a controller would call this method on a widget for making a pie chart once to establish an event listener which would then kick off the prepChart method on the widget whenever the browser was resized, thus resizing the pie chart the widget displayed. The problem was though that the event was never turned back off and once one transitioned to another view errors starting bubbling up to the console. The postCreate method ended up getting refactored to:

postCreate: function() {
   this.resizeCallback = lang.hitch(this, this.prepChart);
   window.addEventListener('resize', this.resizeCallback, false);
},

 
 

In the beforeDeactivate method on a controller that called the postCreate method in its afterActive method, one would have to call out to a new method on the widget to turn the event listener back off:

dropEvent: function() {
   window.removeEventListener('resize', this.resizeCallback, false);
},

 
 

Note how lang.hitch had to get cast to a variable and then used from there. Calling lang.hitch twice somehow produced a mismatch, so the cast-to-a-variable trick was the way around that problem.

No comments:

Post a Comment