CoffeeScript函数编译错误并早期返回

时间:2012-09-29 00:14:15

标签: javascript coffeescript

我在CoffeeScript中编写了一些代码,我无法弄清楚如何以正确编译的方式编写代码。

    f = (id) -> 
        matching = app.list.where
            id: id

        if matching.length == 0
            success = (data)->
                if not data[id]
                    alert "couldn't find id " + id
                else
                    b = new Thing data[id]
                    b.set 'id',id 
                    b.trigger 'select'

            error = ->

            app.api.request 'info','GET',success,error,
                ids: id
        else
            b = matching[0]
            b.trigger 'select'

正在编译:

  f = function(id) {
    var matching;
    return matching = app.list.where({
      id: id
    });
  };
  if (matching.length === 0) {
    success = function(data) {
      var b;
      if (!data[id]) {
        return alert("couldn't find id " + id);
      } else {
        b = new Thing(data[id]);
        b.set('id', id);
        return b.trigger('select');
      }
    };
    error = function() {};
    app.api.request('info', 'GET', success, error, {
      ids: id
    });
  } else {
    b = matching[0];
    b.trigger('select');
  }

如您所见,只有matching的分配被解析为f的一部分。

1 个答案:

答案 0 :(得分:0)

f = (id) -> 
    matching = app.list.where id: id
    if matching.length == 0
        success = (data)->
            if not data[id]
                alert "couldn't find id " + id
            else
                b = new Thing data[id]
                b.set 'id',id 
                b.trigger 'select'

        error = ->

        app.api.request 'info','GET',success,error,
            ids: id
    else
        b = matching[0]
        b.trigger 'select'