【发布时间】:2014-04-21 01:05:17
【问题描述】:
我正在尝试在 coffeescript 中编写一个简单的 github API 调用,但在 coffeescript.org 和 coffee-rails gem 之间遇到了我无法解释的不一致。我检查了多个选项卡/空格组合的次数超出了我的计数,并且觉得一定有一些微妙或愚蠢的东西我遗漏了。
原始代码
$ ->
gistids = ['5100088']
for gistid in gistids
$.ajax
url: 'https://api.github.com/gists/'+gistid,
type: 'GET',
dataType: 'jsonp'
.success (gistdata) ->
console.log(gistdata.data.files)
.fail (e) ->
console.log(e)
Coffeescript.org 编译
它在 coffeescript.org 上很高兴地编译为以下内容(通过控制台运行时,功能完全符合预期)。
$(function() {
var gistid, gistids, _i, _len, _results;
gistids = ['5100088'];
_results = [];
for (_i = 0, _len = gistids.length; _i < _len; _i++) {
gistid = gistids[_i];
_results.push($.ajax({
url: 'https://api.github.com/gists/' + gistid,
type: 'GET',
dataType: 'jsonp'
}).success(function(gistdata) {
return console.log(gistdata.data.files);
}).fail(function(e) {
return console.log(e);
}));
}
return _results;
});
Rails-coffee gem 编译(这是我的输出)
替代编译显然注定要失败,而且似乎与我想要实现的目标相去甚远。看起来for 循环正在尝试按照您在eat food for food in ['toast', 'cheese', 'wine'] 类型语法中所期望的那样执行内容?
如果你使用另一个在线的 'try coffeescript' 类型的站点,也会输出此代码,例如http://www.compileonline.com/try_coffeescript_online.php
$(function() {
var gistid, gistids, _i, _len, _ref, _results;
_ref = gistids($.ajax({
url: 'https://api.github.com/gists/' + gistid,
type: 'GET',
dataType: 'jsonp'.success(function(gistdata) {
return console.log(gistdata.data.files).fail(function(e) {
return console.log(e);
});
})
}));
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
gistid = _ref[_i];
_results.push(gistids = ['5100088']);
}
return _results;
});
我错过了什么?正在使用的版本之间是否有重大变化、编译标志差异、被忽略的小语法错误,还是我完全弄错了?
【问题讨论】:
标签: javascript ruby-on-rails compilation coffeescript