Setting the right context on delay (backbone/underscore)
I have been working on a backbone based project recently and had a situation where i wanted an event to emit only once that too with some restrictions. The way I thought to do it was to do it with an dealy and clearTimeout when required by the state of the application. Everything was fine untill i figured out that the function passed to _.delay
doesnt bind to this
like the rest of backbone application does, generally.
This is how I solved it, thanks to this issue on github.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_(function() { | |
this.trigger("awesome:event"); | |
}).chain().bind(this).delay(500); | |
// Other ways of doing it in the github page | |
//https://github.com/jashkenas/underscore/issues/494 |
Will keep posting on more findings, good to look back when i forget :D