2015-11-10 88 views
0

我寫在CoffeeScript中的原子的插件,我使用的是this nodejs API (telegram.link)。它有一些不對稱函數,所以我必須將函數作爲參數傳遞,稱爲回調函數。我的問題是,當我使用下面的代碼:CoffeeScript的功能參數Node.js的API

login: -> 
    console.log 'Loging in' 
    @client = telegramLink.createClient({ 
     id: config.telegram.prod.app.id, 
     hash: config.telegram.prod.app.hash, 
     version: '0.0.1', 
     lang: 'en' 
    }, 
    config.telegram.prod.primaryDataCenter); 
    @client.createAuthKey(@authCallback) 
    console.log @client 

authCallback: (auth) -> 
    console.log auth 
    @client.auth.sendCode(config.telegram.test.telNr, 5, 'en', @sendCodeCallback) 

其被編譯到:

login: function() { 
    console.log('Loging in'); 
    this.client = telegramLink.createClient({ 
    id: 12345, 
    hash: 'q1w2e3r4t5y6u7i8o9p0', 
    version: '0.0.1', 
    lang: 'en' 
    }, config.telegram.prod.primaryDataCenter); 
    this.client.createAuthKey(this.authCallback); 
    return console.log(this.client); 
}, 
authCallback: function(auth) { 
    console.log(auth); 
    return this.client.auth.sendCode(config.telegram.test.telNr, 5, 'en', this.sendCodeCallback); 
} 

@client在authCallback功能是不明確的。

我讀#2(CoffeeScripts classes - access to property in callback),我應該用=>(脂肪箭頭),所以我在下面的編譯腳本試圖由此產生:

authCallback: (function(_this) { 
    return function(auth) { 
    console.log(auth); 
    return _this.client.auth.sendCode(config.telegram.test.telNr, 5, 'en', _this.sendCodeCallback); 
    }; 
})(this) 

@client仍然不確定。我認爲,API的回調函數調用可能無法正常工作了。

還有什麼我可以做,以保持原有的範圍,但要使用該API?

+0

如何調用authCallback?請記住,'this'在(Java |咖啡)腳本函數取決於函數是如何被調用,而不是如何或在哪裏它被定義(除了當然的約束功能)。使用'=>'應該可以做到。你看過'authCallback'裏面有什麼'@'嗎?你確定你首先調用'login'嗎?你確定你在同一個對象上調用'login'和'authCallback'嗎? –

+0

@ muistooshort從API的源代碼'if(callback){this.client.once('sendCode',callback); }'。 API是用純nodejs編寫的,而不是在CoffeeScript中,所以根本就沒有'@'。是的,我確定我首先調用了登錄,因爲它是由Atom中的菜單按鈕觸發的。如果你想看到我的整個項目代碼,我已經在[Github]上分享了它(https://github.com/Morosko/atom-telegram) – Morosko

+0

'@'是CoffeeScript對'this'的簡寫,所以'@'在那裏。我不知道'this.client.once('sendCode',callback)'做了什麼,所以你不得不深入挖掘。或者你可以在'authCallback'中'console.log(@)'並親自查看。 –

回答

0

解決方案:

今天我找到了我的問題解決方案。 現在我的代碼看起來像這樣:

login: -> 
console.log 'Logging in' 
@client = 
    telegramLink.createClient({ 
    id: config.telegram.prod.app.id 
    hash: config.telegram.prod.app.hash 
    version: '0.0.1' 
    lang: 'en' 
    } 
    config.telegram.prod.primaryDataCenter) 
@client.createAuthKey((auth) => 
    console.log @client.auth 
    @client.auth.sendCode(
    config.telegram.test.telNr, 
    5, 
    'en', 
    @sendCodeCallback)) 
console.log @client 
0

當我看着你的代碼的indetion,我猜你是不是類裏面。如果你不在類的實例中,那麼「this」或「@」將不起作用。第二件事是回調的定義。它不應該被定義爲你的班級的一種方法。這個類可以是這個樣子:

class MyClass 
    constructor: -> 
    // do something 
    login: -> 
    @client = telegramLink.createClient({ 
     id: config.telegram.prod.app.id, 
     hash: config.telegram.prod.app.hash, 
     version: '0.0.1', 
     lang: 'en' 

    @client.authCallback: (auth) => 
     console.log auth 
     @client.auth.sendCode(config.telegram.test.telNr, 5, 'en', @sendCodeCallback) 

那類事情的作品,你需要這樣也許創建一個實例,是不是你想要的。

你可以改變你的代碼,它就像你剛纔用的功能來定義它。

login: -> 
    console.log 'Loging in' 
    client = telegramLink.createClient({ 
     id: config.telegram.prod.app.id, 
     hash: config.telegram.prod.app.hash, 
     version: '0.0.1', 
     lang: 'en' 
    }, 
    config.telegram.prod.primaryDataCenter); 
    client.createAuthKey(@authCallback) 
    console.log @client 

authCallback: (auth) -> 
    console.log auth 
    client.auth.sendCode(config.telegram.test.telNr, 5, 'en', sendCodeCallback) 

提示:createClient方法內的選項字典的定義看起來像JS,而不像Coffescript。另外「;」你不應該使用。 混合的定義也可能造成一些錯誤。

+0

謝謝您的回答。我的代碼在一個類中。感謝JS和CoffeeScript的混合提示。我只是從API的文檔中複製它。 – Morosko