The dygraph drawcallback gets two arguments one the reference to the dygraph object and other being a boolean if its the first time the graphs being drawn. But I also want to send optional arguments to the callback instead of creating global variables. Is that possible?
Your question is not specific to dygraphs. Rather, this is a common need when using JavaScript. Common techniques:
JavaScript (closure)
var self = this;
drawCallback : function(dygraph, is_init) {
self._myCallbackFunction(extraParam1, extraParam2, dygraph, is_init)
}
The this
context might not be what you expect in your callback function though. If you want to set this
to the context of the object where the callback function is defined, then you can use:
JavaScript call
or apply
functions
drawCallback : function(dygraph, is_init) {
self._myCallbackFunction.call(self, extraParam1, extraParam2, dygraph, is_init)
}
or jQuery (.proxy)
drawCallback : $.proxy(this._myCallbackFunction, this, extraParam1, extraParam2)