2014-10-03 89 views
0

在下面的一些API中,它需要我想在構造函數中動態分配的憑據。然後我想在整個班級中使用一些API。即在下面的例子中someMethodUsingSomeAPI是一個幫助方法,我想從B的一個實例中的其他方法中調用。這對Coffee-/JavaScript來說可能嗎? (我可以讓它開始工作的唯一方法是,如果我把someMethodUsingSomeAPI在構造函數中。)從構造函數實例化的全範圍對象

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 

已更新,建議從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! 

更改someMethodUsingSomeAPI:someMethodUsingSomeAPI =

console.log someMethodUsingSomeAPI '123' # Error: unsupported argument list 

&

b = B('username') 
b.someMethodUsingSomeAPI '123' # TypeError: Object #<B> has no method 'someMethodUsingSomeAPI' 

(這與流星0.9.3.1)

UPDATE IN試圖澄清

Here's a simplified version of the above, without any of the API stuff.

someMethod = workssomeMethod: doesn't work

我很高興classInstance.someMethod在使用時有效,但真的很想讓它在實際情況下工作。

+0

爲什麼讓'someAPI'靜態變量類之外,而不是實例*物業,*? – Bergi 2014-10-03 00:45:27

+0

請注意,'JSON.parse'確實需要一個JSON字符串,而不是文件路徑。 – Bergi 2014-10-03 00:45:52

+0

對,對不起。爲簡潔起見,刪除了一些內容。加回來。 – jiku 2014-10-03 01:02:05

回答

0

當然,您可以將someAPI附加到您的類對象上。在咖啡標@用於代替this(因爲它會在Javscript中)。

SomeAPI = require 'someAPI' 

class A extends B 
    constructor: (options = {}) -> 
    unless @ instanceof A 
     return new A(options) 

    @config = JSON.parse('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: (id, callback) -> 
    return @someAPI.get 'whatever/show', { 'id': id }, callback 

你可以看看this SO question這也解釋了JavaScript的this,其範圍。一旦你明白了,看看coffeescript's classes是如何工作的,你應該對@的使用有一定的瞭解。

+0

謝謝。我想我嘗試了這一點,在嘗試在構造函數之外聲明一些API之前,嘗試了這些。更新原始文章以反映我正在做什麼,但是我仍然得到相同的錯誤,即'Can not call method'get'of undefined'。 – jiku 2014-10-03 01:07:06

0

我想這是你想要什麼:

someMethodUsingSomeAPI:function(id){ 
    var wrappedGet=Async.wrap(@someAPI,"get"); 
    return wrappedGet("whatever/show",{ 
    id:id 
    }); 
} 
+0

這對於b.someMethodUsingSomeAPI非常有效,但除非我搞砸了CS,否則它在實例內不起作用。結果更新了上面的例子。 – jiku 2014-10-03 02:32:58

+0

在定義對象文字的新屬性時,請刪除'='而不是':',因爲如果你不這樣做,它絕對沒有SENSE,我不知道地球上的CS怎麼沒有警告你。如果沒有「新」的B這些instanciations?您是否在其他方法中使用正確的語法調用了一些Method,使用了SomeAPI,即'@ someMethodUsingSomeAPI'? – saimeunt 2014-10-03 02:54:03

+0

好的。我不知道爲什麼,但使用=我實際上有'console.log someMethodUsingSomeAPI'123''工作,如果我把兩者都在構造函數。 B wit(out)new似乎沒有什麼區別。只要返回一個新的,除非已經在構造函數中聲明瞭B的一個實例。如果我做了'console.log @someMethodUsingSomeAPI'123'',我得到了'TypeError:Object#沒有方法'someMethodUsingSomeAPI'' – jiku 2014-10-03 03:05:11

0

基於你在這裏提供的簡單的例子是應該符合您要求的版本:

class SomeClass 
    # Store the last created instance at the class level 
    lastInstance: null 

    constructor: (options = {}) -> 
    @someLastName = options.someLastName 
    # In constructor, the created instance is now the lastInstance 
    SomeClass.lastInstance = this 

# The helper method is not created in the class scope 
someMethod = (someFirstName) -> 
    "Hello " + someFirstName + " " + SomeClass.lastInstance.someLastName 

someClass = new SomeClass({ someLastName: 'Borgnine' }) 

alert someMethod 'Ernest' 

而且here's a link to try it in a browser

基本上,正如你所期望的幫助方法訪問類的一個實例,你需要提供一個引用幫助者可以在某個時候引用該實例。在這裏,我使用一個類變量來存儲上次創建的實例,以及最後一次調用時的輔助訪問。這意味着,創造了許多實例時,將如下進行:

someClass = new SomeClass({ someLastName: 'Borgnine' }) 

alert someMethod 'Ernest' # Hello Ernest Borgnine 

someClass = new SomeClass({ someLastName: 'Doe' }) 

alert someMethod 'Boris' # Hello Boris Doe 
相關問題