解析参数& CoffeeScript中的匿名函数

时间:2011-06-24 09:43:55

标签: javascript facebook 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'}
    );
}

最感谢...

加雷

修改

感谢下面的所有答案,有些人没有做过,有些人给我带来了其他问题。

我已经确定了解决方案的混合搭配:

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

感谢所有回答的人。我希望这有助于其他人!

亲切的问候,

加雷

6 个答案:

答案 0 :(得分:7)

当我抓住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"

答案 1 :(得分:0)

这是未经测试的,但我认为以下内容应该有效:

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'

答案 2 :(得分:0)

当我遇到这些问题时,我做的两件事就是:

  1. 添加很多括号。
  2. 将每个参数放在局部变量中。
  3. 在这种情况下,括号有帮助:

    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
    

答案 3 :(得分:0)

A FAQ

FB.login (response) ->
  if response.perms and response.session
    cbk true, response
  # ...
, perms: 'email,publish_stream'

最后一行的dedent +逗号可以解决问题。

答案 4 :(得分:0)

为了便于阅读,我肯定会将您传递给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

每个函数末尾的额外return可能也可能无关紧要,具体取决于API。请注意你的隐含回报。

答案 5 :(得分:0)

这将编译为功能相当的代码,具有更短的优势。我建议在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

根据这些函数的返回值是否重要,可能不需要返回。