2015-11-10 48 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