[英]Parsing Arguments & Anonymous functions in CoffeeScript
How can I turn this function into a coffeescript compatible version? 如何将该功能转换为兼容CoffeeScript的版本?
My main issue is with fb.login.. however I try I always seem to lose either one of the arguments or the permissions... 我的主要问题是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'}
);
}
Most appreciated... 非常感谢...
Gareth 加雷思
EDIT 编辑
Thanks for all of your answers below, some worked some didn't, some caused me other issues. 感谢您在下面提供的所有答案,有些工作没有完成,有些导致了我其他问题。
I have settled on a mix and match of the solutions: 我已经确定了各种解决方案的搭配:
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
Thank you to all who answered. 谢谢所有回答的人。 I hope this helps others!
希望这对其他人有帮助!
Kind regards, 亲切的问候,
Gareth 加雷思
I always use js2coffee when I grab js snippets and want a quick coffeescript conversion. 当我获取js代码片段并想要快速的coffeescript转换时,我总是使用js2coffee。
http://ricostacruz.com/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"
and then iterate out the obvious 然后迭代明显的
promptLogin = (login_level, callback) ->
cbk = callback
FB.login (response) ->
if response.session
cbk response.perms, response
else
cbk false, response
, perms: "email,publish_stream"
and again 然后再次
promptLogin = (login_level, callback) ->
FB.login (response) ->
if response.session
callback response.perms, response
else
callback false, response
, perms: "email,publish_stream"
till finally you get 直到最后你得到
promptLogin = (login_level, callback) ->
FB.login (response) ->
callback
if response.session then response.perms else false
response
, perms: "email,publish_stream"
It's untested, but I think the following should work: 它未经测试,但我认为以下方法可以工作:
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'
When I encounter these problems, the two things I do are: 当我遇到这些问题时,我要做的两件事是:
In this case, parentheses help: 在这种情况下,括号可以帮助:
promptLogin = (login_level, cb) ->
FB.login(((response) ->
cb response.perms and response.session, response),
perms: 'email, publish-stream')
And temporary variables help: 临时变量有助于:
promptLogin = (login_level, cb) ->
login = (response) -> cb response.perms and response.session, response
options = perms: 'email, publish-stream'
FB.login login, options
I would definitely name the callback that you're passing to FB.login
for the sake of readability: 为了便于阅读,我绝对会命名为传递给
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
The extra return
s at the end of each function may or may not matter, depending on the API. 每个函数末尾的额外
return
可能无关紧要,具体取决于API。 Just be aware of your implicit returns. 只要注意您的隐性回报。
This compiles to what appears to be functionally equivalent code, with the advantage of being much shorter. 这可以编译为功能上等效的代码,并且具有更短的优势。 I would suggest using the coffeescript real-time compiler at http://jashkenas.github.com/coffee-script/ for testing things like this.
我建议使用http://jashkenas.github.com/coffee-script/上的coffeescript实时编译器来测试类似的东西。
promptLogin = (login_level, callback) ->
FB.login (response) ->
callback response.session and response.perms, response
return
, perms:'email,publish_stream'
return
The returns may be unnecessary depending on whether the return values of those functions matter. 根据那些函数的返回值是否重要,返回可能是不必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.