Javascript到Coffeescript的转换不需要';行不通

Javascript到Coffeescript的转换不需要';行不通,coffeescript,Coffeescript,我有下面的函数,它在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) { c

我有下面的函数,它在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
}
函数获取数据(目标){
return context.metric(函数(开始、停止、步骤、回调){
var fetch_time=new Date().getTime();
if((获取时间-当前时间)<6000&&saved\u data.length>0){
回调(null,graphite_parse(保存的_数据));
}
否则{
//------开始请求新数据------------------
d3.json(主机+“/render?format=json”
+“&target=“+encodeURIComponent(目标)
+“&from=“+graphite_格式_日期(开始-2*步)
+“&until=“+graphite\u format\u date(停止-1000),
功能(数据){
如果(!data)返回回调(新错误(“无法加载数据”);
当前时间=提取时间
保存的数据=数据;
回调(null,graphite_解析(数据));
});
//------结束请求新数据--------------------
}//否则
});//返回
}
当我尝试使用将其转换为coffeescript时,它不起作用,我不确定如何调试:

    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 --------------------
graphite\u get\u data=(目标)->
控制台日志(全局_pod)
console.log(“hi”)打印
context.metric(开始、停止、步骤、回调)->
console.log(“hi”)不打印
fetch_time=new Date().getTime()
如果(获取时间-当前时间)<6000且保存的数据长度>0
回调null,graphite_parse(保存的_数据)#将使用全局变量test_pod
其他的
#------开始请求新数据------------------
d3.json host+“/render?format=json”+“&target=“+encodeURIComponent(target)+”和from=“+graphite_format_date(start-2*step)+”和until=“+graphite_format_date(stop-1000),(data)->
返回回调(新错误(“无法加载数据”)),除非数据
当前时间=提取时间
保存的数据=数据
回调null,graphite_parse(数据)#将使用全局变量test_pod
#------结束请求新数据--------------------
你能告诉我如何调试咖啡脚本吗

编辑:我检查了生成的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);
graphite\u get\u data=函数(目标){
var提取时间;
控制台日志(“hi”);
metric(函数(开始、停止、步骤、回调){});
fetch_time=new Date().getTime();
控制台日志(“hi”);
if((获取时间-当前时间)<6000&&saved\u data.length>0){
回调(null,graphite_parse(保存的_数据));
返回true;
} 
否则{
d3.json(主机+“/render?format=json”+”&target=“+encodeURIComponent(target)+”和from=“+graphite_format_date(start-2*step)+”&till=“+graphite_format_date(stop-1000),函数(data){
如果(!数据){
返回回调(新错误(“无法加载数据”);
}//如果结束,则结束
当前时间=提取时间;
保存的数据=数据;
回调(null,graphite_解析(数据));
返回true;
});//结束函数(数据)
返回true;
}//结束其他
//缺失});
};
graphite_格式_日期=函数(时间){
返回数学楼层(时间/1000);
};
返回函数(数据){
var pod_数据、pod_json_数据;
pod_json_data=$.grep(数据,函数(e){returne e.target===test_pod;});
pod_data=pod_json_data[0][“datapoints”].slice(1).map(函数(d){返回d[0];});
返回pod_数据;
};
}
}); // 这是什么??它应该是失踪的
}).打电话(这个);
发现问题是});一处缺失,另一处添加错误

仍在研究如何修复它

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

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

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

当然可以:使用。请记住,有时候咖啡中的一个步骤就是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)
这句话在你的版本中没有出现,但在我的版本中很好。我不知道为什么,我怀疑你在咖啡代码中遗漏了一些缩进咖啡脚本中的缩进非常重要。


我还建议尝试使用Chrome——js2coffee是为流量付费的,而不是得到任何东西

咖啡脚本的开头似乎有缩进问题

这就是生成第二个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 ...
  )

啊,我讨厌咖啡!不知道rails为什么喜欢它。它实际上非常棒,但你应该好好学习,自己编写,而不是通过一个自动的JavaScript->CoffeeScript转换器,它将输出非常可疑的代码。尝试使用sourcemaps调试CoffeeScript@nemo“不知道rails为什么喜欢它”——因为它与RubyWhy超级相似——为什么不把它作为JavaScript呢?用一个工具将JavaScript转换为CoffeeScript,然后用另一个工具将CoffeeScript转换回JavaScript,我觉得很奇怪。js2coffee似乎有点问题,你最好学习CoffeeScript并用大脑转换代码(或者干脆把它作为JavaScript)。是的,你是对的,我只是觉得我应该遵循一切都是coffeescript的标准,因为Rails的人认为某件事是个好主意并不意味着这是个好主意,也不意味着你必须遵循。固执己见并不意味着正确,Rails的人只是固执己见,有时是正确的。是的,谢谢!我会给这30分钟,如果它不工作,我会把它作为JS。。。谢谢好的,我现在正在使用JS。
graphite_get_data = (target) ->
  context.metric( (start, stop, step, callback) ->
    fetch_time = new Date().getTime()
    if ...
    else ...
  )