最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing Arguments & Anonymous functions in CoffeeScript - Stack Overflow

programmeradmin4浏览0评论

How can I turn this function into a coffeescript patible version?

My main issue is with fb.login.. however I try I always seem to lose either one of the arguments or the permissions...

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

How can I turn this function into a coffeescript patible version?

My main issue is with fb.login.. however I try I always seem to lose either one of the arguments or the permissions...

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

Share Improve this question edited Jun 29, 2011 at 18:49 ThinkGareth asked Jun 24, 2011 at 9:43 ThinkGarethThinkGareth 551 silver badge6 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 7

I always use js2coffee when I grab js snippets and want a quick coffeescript conversion.

http://ricostacruz./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"

I would definitely name the callback that you're passing to FB.login for the sake of readability:

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 returns at the end of each function may or may not matter, depending on the API. Just be aware of your implicit returns.

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:

  1. Add a lot of parentheses.
  2. Put each of the arguments in local variables.

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

A FAQ.

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

The dedent + ma on the last line does the trick.

This piles to what appears to be functionally equivalent code, with the advantage of being much shorter. I would suggest using the coffeescript real-time piler at http://jashkenas.github./coffee-script/ for testing things like this.

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.

发布评论

评论列表(0)

  1. 暂无评论