解析参数&CoffeeScript中的匿名函数
Parsing Arguments & Anonymous functions in CoffeeScript
如何将此函数转换为与coffeescript兼容的版本?
我的主要问题是fb.login。然而,我尝试过了,我似乎总是失去其中一个参数或权限。。。
function promptLogin(login_level, callback)
{
var cbk = callback;
FB.login(function(response) {
if (response.session) {
if (response.perms) {
cbk( true, response );
} else {
cbk( false, response );
//showAlert( "Please accept the permissions.", 'Error' );
}
} else {
cbk( false, response );
//showAlert( "Please accept the permissions.", 'Error' );
}
}, {perms:'email,publish_stream'}
);
}
非常感谢。。。
Gareth
编辑
感谢您在下面给出的所有答案,有些有效,有些无效,有些给我带来了其他问题。
我已经确定了一个混合和匹配的解决方案:
login: (permissions = '', callback) ->
responseHandler = (response) ->
if response.session
if permissions && permissions != ''
if permissions == response.perms
AP.log 'Login accepted - Permissions Accepted', response
callback true, response
else
AP.log 'Login rejected - Permissions Rejected', response
callback false, response
else
AP.log 'Login accepted - No Permissions', response
callback true, response
else
AP.log 'Login rejected', response
callback false, response
return
FB.login responseHandler, perms: permissions
感谢所有回答的人。我希望这能帮助其他人!
问候,
Gareth
当我获取js片段并想要时,我总是使用js2coffee咖啡脚本的快速转换。
http://ricostacruz.com/js2coffee/
promptLogin = (login_level, callback) ->
cbk = callback
FB.login (response) ->
if response.session
if response.perms
cbk true, response
else
cbk false, response
else
cbk false, response
, perms: "email,publish_stream"
然后迭代出明显的
promptLogin = (login_level, callback) ->
cbk = callback
FB.login (response) ->
if response.session
cbk response.perms, response
else
cbk false, response
, perms: "email,publish_stream"
再次
promptLogin = (login_level, callback) ->
FB.login (response) ->
if response.session
callback response.perms, response
else
callback false, response
, perms: "email,publish_stream"
直到最后你得到
promptLogin = (login_level, callback) ->
FB.login (response) ->
callback
if response.session then response.perms else false
response
, perms: "email,publish_stream"
为了可读性,我肯定会将您传递给FB.login
的回调命名为:
promptLogin = (login_level, callback) ->
responseHandler = (response) ->
if response.session
if response.perms
callback true, response
else
callback false, response
# showAlert 'Please accept the permissions.', 'Error'
else # no session
callback false, response
# showAlert 'Please accept the permissions', 'Error'
return
FB.login responseHandler, perms: 'email,publish_stream'
return
根据API,每个函数结尾的额外return
可能重要,也可能无关紧要。只要注意你隐含的回报。
它未经测试,但我认为以下内容应该有效:
promptLogin = (login_level, callback) ->
FB.login ((response) ->
if response.perms and response.session
cbk true, response
else
if not response.perms
cbk false, response
# showAlert ...
else # if not response.session
cbk false, response),
perms: 'email,publish_stream'
当我遇到这些问题时,我会做两件事:
- 加上许多括号
- 将每个参数放入局部变量中
在这种情况下,括号有助于:
promptLogin = (login_level, cb) ->
FB.login(((response) ->
cb response.perms and response.session, response),
perms: 'email, publish-stream')
临时变量有帮助:
promptLogin = (login_level, cb) ->
login = (response) -> cb response.perms and response.session, response
options = perms: 'email, publish-stream'
FB.login login, options
常见问题解答。
FB.login (response) ->
if response.perms and response.session
cbk true, response
# ...
, perms: 'email,publish_stream'
最后一行的dedent+逗号就可以了。
这可以编译成功能等效的代码,其优点是更短。我建议在http://jashkenas.github.com/coffee-script/用于测试这样的东西。
promptLogin = (login_level, callback) ->
FB.login (response) ->
callback response.session and response.perms, response
return
, perms:'email,publish_stream'
return
返回可能是不必要的,这取决于这些函数的返回值是否重要。
相关文章:
- 尝试将js函数更改为coffeescript时发生Uncaught ReferenceError
- 如何在函数中调用函数?(CoffeeScript)
- Angularjs 与 coffeescript 函数表达式中断
- 调用命名空间的公共函数(coffeescript)时出现问题
- CoffeeScript-传递一个带有参数的匿名函数
- 一旦转换为CoffeeScript,就调用一个普通的JS函数
- 在方法中的函数中引用 CoffeeScript 类中的“this”的最佳方式是什么?
- 在 Coffeescript 类的函数中指定第 n 个参数的“super”
- coffeescript/backline.js:编写一个对字符串进行操作的哈希函数
- 装饰 CoffeeScript 类中的函数
- 从 rails 中的不同 coffeescript 文件调用函数
- 如何在 Rails 3 上从 js.erb 调用 coffeescript 函数以及如何理解 Javascript 中的
- 如何将 JavaScript forEach 循环/函数转换为 CoffeeScript
- 任何转换对象*值*的Javascript或Coffeescript“映射”函数
- CoffeeScript - 将参数传递给超级构造函数时出现问题
- javascript/coffeescript 使用函数哈希作为参数
- CoffeeScript 承诺与函数定义链接
- 导出 node.js / coffeescript 中的私有函数
- 获取函数 coffeescript 中的对象值
- 未捕获类型错误:this.source不是函数coffeescript错误