0

我正在用 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.

4

1 回答 1

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'
于 2012-09-29T00:20:19.013 回答