我有以下在 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);
发现问题是 }); 在一个地方丢失并在另一个错误的地方添加
仍在研究如何解决它