With someAPI in the following, it requires credentials that I would like to assign dynamically in the constructor. Then I would like to use someAPI throughout the class. I.e. in the following example someMethodUsingSomeAPI is a helper method I would like to call from other methods within an instance of B. Is that possible with Coffee-/JavaScript? (The only way I can get it to work is if I put someMethodUsingSomeAPI inside the constructor.)
SomeAPI = Npm.require 'someAPI'
class B extends A
constructor: (options = {}) ->
unless @ instanceof B
return new B(options)
@config = JSON.parse(Assets.getText('config/' + options.username + '.json'))
@someAPI = new SomeAPI
consumer_key: @config.credentials.consumer.key
consumer_secret: @config.credentials.consumer.secret
access_token: @config.credentials.access.token
access_token_secret: @config.credentials.access.secret
someMethodUsingSomeAPI = Async.wrap((id, callback) ->
return @someAPI.get 'whatever/show', { 'id': id }, callback
)
console.log someMethodUsingSomeAPI '123' # Error: Cannot call method 'get' of undefined
Updated with suggestion from saimeunt
...
someMethodUsingSomeAPI = (id) ->
wrappedGet = Async.wrap(@someAPI, 'get')
wrappedGet 'whatever/show', { id: id }
console.log someMethodUsingSomeAPI '123' # ReferenceError: someMethodUsingSomeAPI is not defined
&
b = B('username')
b.someMethodUsingSomeAPI '123' # Works!
Changing someMethodUsingSomeAPI:
to someMethodUsingSomeAPI =
console.log someMethodUsingSomeAPI '123' # Error: unsupported argument list
&
b = B('username')
b.someMethodUsingSomeAPI '123' # TypeError: Object #<B> has no method 'someMethodUsingSomeAPI'
(This is with Meteor 0.9.3.1)
UPDATE IN AN ATTEMPT TO CLARIFY
Here's a simplified version of the above, without any of the API stuff.
someMethod = works, someMethod: doesn't work
I'm happy that classInstance.someMethod works when using :, but would REALLY like to have it work in the actual instance.