I am writing a simple Twitter client to play with coffeescript. I have an object literal with some functions that call each other through callbacks.
somebject = foo: 'bar' authenticateAndGetTweets: -> console.log "Authorizing using oauth" oauth = ChromeExOAuth.initBackgroundPage(this.oauthdetails) oauth.authorize( this.afterLogin.call this ) afterLogin: -> this.getTweets(this.pollinterval)
This code works just fine. Edit: in fact, this.afterlogin should be sent as a callback above, and not immediately started, as Trevor noted below.
If in authenticateAndGetTweets I deleted the “call” and just ran:
oauth.authorize( this.afterLogin )
and do not use 'call', I get the following error:
Uncaught TypeError: Object [object DOMWindow] has no method 'getTweets
Which makes sense, since 'this' in afterLogin is tied to what triggered the callback, not the "some object" of my object literal.
I was wondering if there was any magic in Coffeescript that I could do instead of a “call”. I originally thought using '=>', but the code will give the same error as above if '=>' is used.
So, is there a way to avoid using a call? Or coffeescript does not eliminate the need? What did '=>' not work as I expected?
Thanks. I still like the coffee maker and want me to do everything right. "
source share