标签: coffeescript

Rails 4控制器没有返回格式良好的JSON

我正在使用Rails 4构建一个Web应用程序,我偶然发现了Rails控制器和JQuery之间的一些奇怪的行为,我不确定是不是因为我做错了什么,或者是否真的误解了Rails通过JQuery返回JSON.请参阅以下咖啡脚本:

咖啡脚本:

$.get('/get_experiment', (data) -> 
  console.log 'Success:'
  console.log JSON.parse data
).error (data) ->
  console.log 'Failure:'
  console.log JSON.parse data.responseText
Run Code Online (Sandbox Code Playgroud)

我们只需通过GET调用控制器的方法,处理成功和失败并显示返回的数据.

控制器:

def get_experiment
  respond_to do |format|
    format.js { render json: {} }
  end
end
Run Code Online (Sandbox Code Playgroud)

该控制器简单地回复一个空哈希.


运行此示例时,控制台将显示带有"Failure"文本的空JSON,这显然表明它认为JSON格式不正确.

我不明白为什么空哈希被认为是格式错误的JSON.如果用render json: {foo: 'bar'}它替换它,它仍然会调用error方法.

ruby jquery json ruby-on-rails coffeescript

2
推荐指数
1
解决办法
1367
查看次数

意外的字符串错误需要在JS App文件中找不到Express

我有一个用快递,节点,套接字io和coffeescript编写的js web应用程序.我想在本地计算机上启动本地服务器.

我去了包含该package.json文件的目录并运行npm install以安装包括express的所有依赖项.

当我跑步时,npm list我确实看到了express那里.

当我尝试启动应用程序时

node app.coffee的所在目录内app.coffee位于

它给了我这个错误

exports, require, module, __filename, __dirname) { express = require 'express'
                                                                     ^^^^^^^^^
SyntaxError: Unexpected string
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
Run Code Online (Sandbox Code Playgroud)

我确保我有节点

which node 我在我的机器上安装了咖啡脚本

sudo npm install -g coffee-script

我甚至试过了

coffee app.coffee

我不确定还需要检查什么.该app.coffee是我的文件夹的NodeJS.

node.js coffeescript express

2
推荐指数
1
解决办法
2995
查看次数

coffeescript:压制"ReferenceError"

目前,通过工作教程中使用Backbone.js的与CoffeeScript的.

利用以下index.html文件:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>CoffeeScript, Meet Backbone.js: Part N</title>
  <link rel="stylesheet" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
  <script type="text/javascript" src="./index.js"></script>

</head>
<body>
  <header>
    <h1>CoffeeScript, Meet Backbone.js: Part 1</h1>
  </header>
</body>


</html>
Run Code Online (Sandbox Code Playgroud)

它加载一个index.js从CDN加载骨干,jQuery的,等以后文件.希望在script.coffee我希望通过运行类似的东西自动编译到上面script.js加载的文件的文件中工作index.htmlcoffee script.coffee -c -w.

麻烦的是,ReferenceError当我尝试在以下script.coffee文件上运行上述命令时,我正在尝试:

jQuery ->

  class ListView extends Backbone.View

    el: $ 'body'

    initialize: ->

      _.bindAll @
      @render()

    render: ->
      $(@el).append '<ul><li>Hello, Backbone!</li></ul>'

  list_view …
Run Code Online (Sandbox Code Playgroud)

javascript jquery coffeescript backbone.js

2
推荐指数
1
解决办法
2019
查看次数

grunt.util.spawn不提供输出

Grunt.util.spawn没有调用done函数.这是代码.当命令从命令提示符执行时它失败并抛出一条错误消息,该消息未在gruntjs中捕获.发生了什么

module.exports = (grunt)->
grunt.initConfig
    concurrent:
        dev: ['watch']
    slim:
        dev:
            expand: true
            src:  'views'
            dest: 'views/'
    watch:
        slim:
            files: 'views/**.slim'
            tasks:['slim']

grunt.registerMultiTask 'slim', 'Server Slim Compiler', ->
    console.log 'In the slim'
    options = {}
    options.cmd = 'dirld'
    options.grunt = true
    console.log options
    grunt.util.spawn options, (err,res,cod)->
        console.log 'in the spawn'
        if err
            grunt.log.error err
        else
            grunt.log.oklns 'success'
            grunt.log.writeln res

grunt.loadNpmTasks 'grunt-contrib-watch'
grunt.loadNpmTasks 'grunt-concurrent'
grunt.registerTask 'default', ['concurrent:dev']
Run Code Online (Sandbox Code Playgroud)

coffeescript gruntjs

2
推荐指数
1
解决办法
1663
查看次数

在指令属性上使用filter会导致$ digest循环中的无限循环

尝试在具有隔离范围的指令上使用过滤器:

<div tags="p.tags | refTags"></div>
Run Code Online (Sandbox Code Playgroud)

在$ digest循环中导致无限循环:

当应用程序的模型变得不稳定并且每个$摘要周期触发状态更改和随后的$摘要周期时,会发生此错误.Angular检测到这种情况并防止无限循环导致浏览器无响应.

.directive 'tags', ->
    restrict: 'A'
    scope:
        tags: '='
    templateUrl: 'partials/tags.html'


.filter 'refTags', ->
    (tags) ->
        ['a filtered', 'array of values']
Run Code Online (Sandbox Code Playgroud)

谐音/ tags.html

<ul>
    <li ng-repeat="tag in tags">{{tag.tag}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

p.tags在控制器中

p.tags = ['HTML5', 'CSS', 'JavaScript', 'Angular JS', 'Backbone JS', 'Node JS', 'SASS + Compass', 'Oragami', 'Running', 'Cat Food', '#catfood']
Run Code Online (Sandbox Code Playgroud)

这种行为是否正常?

  1. 过滤器不能用于传递给指令的隔离范围的值吗?
  2. 这有解决方法吗?我需要过滤数组值
  3. 是否有另一种设计不同的解决方案?

coffeescript angularjs

2
推荐指数
1
解决办法
1898
查看次数

在nodeJS中运行Mocha测试时出错

我试图让Mocha在nodejs和expressjs中运行.我的测试如下:

assert  = require 'assert'
request = require 'request'
app = require '../../server'

describe "authentication", ->
  describe "GET /login ", ->
    body = null
    before  (done) ->
        options = 
            uri: "http://localhost:3000/login"
        request options, (err, response, _body) ->
            body = _body
            done()
    it "has user field", ->
        assert.ok /user/.test(body)
        # assert.match body, /user/
Run Code Online (Sandbox Code Playgroud)

我在我的server.js文件中添加了coffee-script作为依赖项:

require('coffee-script');

var express = require('express');
var http = require('http');
var path = require('path');

var app = model.exports = express();
Run Code Online (Sandbox Code Playgroud)

我有一个帮助文件_helper.js:

require('coffee-script')
Run Code Online (Sandbox Code Playgroud)

我运行命令:

mocha test/_helper.js test\apps\authentication-test.coffee

这给出了以下错误:

    (exports, …
Run Code Online (Sandbox Code Playgroud)

mocha.js node.js coffeescript express

2
推荐指数
1
解决办法
2070
查看次数

在使用Mocha和Chai断言测试NodeJS代码时,我怎么能期望从回调中抛出异常?

我正在使用Mocha和Chai期望断言来测试我的NodeJS应用程序.我可以写一个这样的测试,它工作得很好:

describe 'My Code', ->
    it 'should handle exceptions', (done) ->
        fn = ->
            # Do something that causes an error
            throw new Error()
        expect(fn).to.throw(Error)
        done()
Run Code Online (Sandbox Code Playgroud)

但是如果fn必须首先调用一个接受回调的函数,并且可能会根据结果抛出错误呢?例如,如果它命中数据库并可能根据结果抛出异常?这不起作用:

describe 'My Callback-using Code', ->
    it 'should handle exceptions thrown from callbacks', ->

        doSomethingAsync = (callback) ->
            setTimeout (->
                callback(somecalculatedvalue)
            ), 1000


        fn = ->
            doSomethingAsync (value) ->
                # The value meets a condition so throw an error
                throw new Error()

        expect(fn).to.throw(Error)
        done()
Run Code Online (Sandbox Code Playgroud)

调用完成后测试结束,但尚未抛出异常,因此测试失败.我怎样才能通过测试?

编辑:根据彼得的回答,这不是正确的方法.这可以用类似于这样的样式更好地表达:

describe 'My Error Handling Code', -> …
Run Code Online (Sandbox Code Playgroud)

unit-testing mocha.js node.js coffeescript chai

2
推荐指数
1
解决办法
918
查看次数

谷歌地图:使用coffeescript异步加载

我正在尝试使用coffeescript 模拟这个异步加载的地图.

这是我的coffeescript:

initialize = ->
  mapOptions =
    zoom: 8
    center: new google.maps.LatLng(-34.397, 150.644)

  map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions)
  return

loadScript = ->
  script = document.createElement("script")
  script.type = "text/javascript"
  script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&" + "callback=initialize"
  document.body.appendChild script
  return


$(window).load ->

loadScript() 
Run Code Online (Sandbox Code Playgroud)

编译为:

(function() {
var initialize, loadScript;

initialize = function() {
  var map, mapOptions;
  mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644)
  };
  map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
};

loadScript = function() {
  var script;
  script = document.createElement("script"); …
Run Code Online (Sandbox Code Playgroud)

javascript google-maps asynchronous coffeescript

2
推荐指数
1
解决办法
674
查看次数

Heroku找不到咖啡脚本来源1.1.3

嗨,我正在开发需要coffee-script-source 1.1.3的 rails应用程序.我知道这个宝石被拉了所以我下载了这个特定版本并将它放在我的供应商/缓存上.我在开发阶段测试它时运行了bundle install 它工作正常但是当把这个项目推送到Heroku时我得到了:

heroku错误

我怎样才能使它工作?谢谢

ruby ruby-on-rails heroku coffeescript

2
推荐指数
1
解决办法
1406
查看次数

2
推荐指数
1
解决办法
76
查看次数