【问题标题】:$.ajax will not work with coffeescript$.ajax 不适用于咖啡脚本
【发布时间】:2023-10-08 22:01:01
【问题描述】:

我有以下代码,我正在尝试用咖啡脚本编写。

$.ajax {
  type: 'GET'
  url: '/dashboard'
  success: (response) -> 
    $('.loading_row').remove()
  dataType: 'script'
}

每次我尝试运行时都会收到以下错误消息:

Assertion failed: (0 && "implement me"), function uv_fs_readlink, file src/unix/fs.c, line 613.

我可以通过将成功回调全部放在一行来解决这个问题,但我希望在回调中调用多个方法,所以这不起作用。

$.ajax {
  type: 'GET'
  url: '/dashboard'
  success: (response) -> $('.loading_row').remove()
  dataType: 'script'
}

【问题讨论】:

  • 您必须进行更多设置...这看起来像 jQuery,但您得到的错误来自 c 文件。所以你是在 Node.js 下运行的,对吧?使用$ = require 'jquery' 而你使用jsdom 是为了让.remove() 行有意义吗?
  • 它在 Rails 3.1 下运行,已包含 jquery,文件中的其他 jquery 行工作正常。不使用 jsdom。
  • OK...所以您在编译时看到此错误?那么,这可能是 ExecJS 使用的 JavaScript 环境的问题。尝试将 therubyracer 添加到您的 Gemfile 并运行 bundle install

标签: jquery coffeescript


【解决方案1】:

根据成功回调是在一行上定义还是缩进,您得到的结果不可能不同。您提供的两个代码 sn-ps 都编译为完全相同的 JavaScript,逐字节:

$.ajax({
  type: 'GET',
  url: '/dashboard',
  success: function(response) {
    return $('.loading_row').remove();
  },
  dataType: 'script'
});

除非您使用的是旧版本的 CoffeeScript?最新版本是 1.1.2。

【讨论】:

  • Rails 3.1 中的coffee-rails gem 所以我认为这是使用最新版本的coffeescript。