【问题标题】:Javascript to Coffeescript conversion doesn't workJavascript 到 Coffeescript 的转换不起作用
【发布时间】:2013-08-14 03:02:36
【问题描述】:

我有以下在 Javascript 中完美运行的函数

function graphite_get_data(target) {
return context.metric(function(start, stop, step, callback) {

var fetch_time = new Date().getTime();
if ((fetch_time-current_time) < 6000 && saved_data.length > 0) {
  callback(null, graphite_parse(saved_data)); 
}
else {
// -------------- Begin Request New Data ------------------
  d3.json(host + "/render?format=json"
      + "&target=" + encodeURIComponent(target)
      + "&from=" + graphite_format_date(start - 2 * step)
      + "&until=" + graphite_format_date(stop - 1000), 
      function(data) {
        if (!data) return callback(new Error("unable to load data"));

        current_time = fetch_time    
        saved_data = data;

        callback(null, graphite_parse(data)); 
      });
// -------------- End Request New Data --------------------
} // else
}); // return
}

当我尝试使用 http://js2coffee.org 将其转换为咖啡脚本时,它不起作用,我不确定如何调试:

    graphite_get_data = (target) ->
  console.log(global_pod)
  console.log("hi") #prints
    context.metric (start, stop, step, callback) ->
    console.log("hi") #doesn't print
    fetch_time = new Date().getTime()
    if (fetch_time - current_time) < 6000 and saved_data.length > 0
      callback null, graphite_parse(saved_data) # will use global variable test_pod
    else
      # -------------- Begin Request New Data ------------------
      d3.json host + "/render?format=json" + "&target=" + encodeURIComponent(target) + "&from=" + graphite_format_date(start - 2 * step) + "&until=" + graphite_format_date(stop - 1000), (data) ->
        return callback(new Error("unable to load data"))  unless data
        current_time = fetch_time
        saved_data = data
        callback null, graphite_parse(data) #will use global variable test_pod
      # -------------- End Request New Data --------------------

你能告诉我如何调试咖啡脚本吗?

编辑:我检查了生成的 javascript 并看到了这个

 graphite_get_data = function(target) {
  var fetch_time;
  console.log("hi");
  context.metric(function(start, stop, step, callback) {});
  fetch_time = new Date().getTime();

  console.log("hi");

  if ((fetch_time - current_time) < 6000 && saved_data.length > 0) {
          callback(null, graphite_parse(saved_data));
          return true;
  } 

  else {
          d3.json(host + "/render?format=json" + "&target=" + encodeURIComponent(target) + "&from=" + graphite_format_date(start - 2 * step) + "&until=" + graphite_format_date(stop - 1000), function(data) {
            if (!data) {
              return callback(new Error("unable to load data"));
            } // end if

            current_time = fetch_time;
            saved_data = data;
            callback(null, graphite_parse(data));
            return true;
          }); //end function(data)

          return true;
        } //end else 
        // missing });
      };

      graphite_format_date = function(time) {
        return Math.floor(time / 1000);
      };

      return graphite_parse = function(data) {
        var pod_data, pod_json_data;

        pod_json_data = $.grep(data, function(e) { return e.target === test_pod; });
        pod_data = pod_json_data[0]["datapoints"].slice(1).map(function(d) { return d[0]; });
        return pod_data;
      };
    }
  }); // what is this here?? it should be in missing

}).call(this);

发现问题是 });在一个地方丢失并在另一个错误的地方添加了

仍在研究如何修复它

【问题讨论】:

  • 你为什么不把它保留为 JavaScript 呢?用一个工具将 JavaScript 转换为 CoffeeScript,然后用另一个工具将 CoffeeScript 转换回 JavaScript 对我来说似乎很奇怪。 js2coffee 似乎比较有问题,你最好学习 CoffeeScript 并用你的大脑转换你的代码(或者把它保留为 JavaScript)。
  • 是的,你是对的,我只是认为我应该遵循资产下所有内容都是咖啡脚本的标准
  • Rails 的人认为某事是个好主意并不意味着它是个好主意或您必须遵循。 固执并不意味着正确,Rails 的人只是固执己见,有时是正确的。
  • 是的,谢谢!如果它不起作用,我会再给它 30 分钟我会保留它作为 JS ......谢谢!
  • 好的,我现在用JS。

标签: coffeescript


【解决方案1】:

看起来咖啡脚本开头有缩进问题。

这是生成第二个 javascript 的原因:

graphite_get_data = (target) ->
  context.metric (start, stop, step, callback) ->
  fetch_time = new Date().getTime()

我的咖啡脚本开始了:

graphite_get_data = (target) ->
  context.metric( (start, stop, step, callback) ->
    fetch_time = new Date().getTime()
    if ...
    else ...
  )

【讨论】:

    【解决方案2】:

    你能告诉我如何调试咖啡脚本吗?

    当然:使用sourcemaps in Chrome。请记住,有时 Coffee 中的一个步骤是 JS 中的几个步骤,因此有时您会看到看起来很奇怪的步进行为。

    关于代码的转换,我认为您可能复制或粘贴了错误的内容,因为它对我来说看起来不错:

    graphite_get_data = (target) ->
      context.metric (start, stop, step, callback) ->
        fetch_time = new Date().getTime()
        if (fetch_time - current_time) < 6000 and saved_data.length > 0
          callback null, graphite_parse(saved_data)
        else
    
          # -------------- Begin Request New Data ------------------
          d3.json host + "/render?format=json" + "&target=" + encodeURIComponent(target) + "&from=" + graphite_format_date(start - 2 * step) + "&until=" + graphite_format_date(stop - 1000), (data) ->
            return callback(new Error("unable to load data"))  unless data
            current_time = fetch_time
            saved_data = data
            callback null, graphite_parse(data)
    

    我发现 js2coffee 非常可靠。

    你的代码最大的问题在这里:

    context.metric(function(start, stop, step, callback) {});
    

    那条线在你的版本中没有出现,但在我的版本中很好。我不确定为什么,我怀疑您在咖啡代码中缺少一些缩进。 coffeescript 中的缩进非常重要。

    我还建议在 Chrome 中尝试 CoffeeScript Enhanced —— js2coffee 为该流量付费,但没有得到任何回报。

    【讨论】:

      【解决方案3】:

      你能告诉我如何调试咖啡脚本吗?

      你不能。调试生成的 JavaScript。 CoffeeScript 不是直接运行的,它会被编译成 JavaScript。

      【讨论】:

      • 啊我讨厌coffeescript!不知道为什么 Rails 喜欢它
      • 它实际上非常棒,但你应该学得足够好,自己编写,而不是通过自动 JavaScript -> CoffeeScript 转换器,它会输出相当有问题的代码。
      • 尝试使用sourcemaps调试coffeescript!
      • @nemo “不确定为什么 rails 喜欢它”——因为它与 Ruby 超级相似
      猜你喜欢
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      相关资源
      最近更新 更多