我想知道是否有办法在咖啡脚本中包含文件.像#include在C或requirePHP中的东西......
我和Yeoman一起运行了很棒的客户端测试.Yeoman编译我的CoffeeScript,在服务器中打开测试页面,使用PhantomJS访问它并将所有测试结果传递给命令行.这个过程非常糟糕,测试结果通过alert()消息传递给Phantom进程,该进程创建一个临时文件,并将消息填充为JSON.Yeoman(好吧,Grunt)遍历临时文件,解析测试并在命令行中显示它们.
我解释这个过程的原因是我想为它添加一些东西.我也得到了服务器端测试.他们使用mocha和supertest来检查API端点和Redis客户端,以确保数据库状态符合预期.但我想合并这两个测试套件!
我不想为服务器调用编写客户端模拟响应.我不想发送服务器模拟数据.在某个地方,我将更改服务器或客户端,测试不会失败.我想做一个真正的集成测试.所以,当在客户端的测试结束我想钩上运行服务器端的相关测试(检查数据库状态,会话状态,移动到不同的测试页).
这有什么解决方案吗?或者,或者,我在哪里开始攻击Yeoman/Grunt/grunt-mocha来完成这项工作?
我认为grunt-mocha中的Phantom Handlers是一个很好的起点:
// Handle methods passed from PhantomJS, including Mocha hooks.
var phantomHandlers = {
// Mocha hooks.
suiteStart: function(name) {
unfinished[name] = true;
currentModule = name;
},
suiteDone: function(name, failed, passed, total) {
delete unfinished[name];
},
testStart: function(name) {
currentTest = (currentModule ? currentModule + ' - ' : '') + name;
verbose.write(currentTest + '...');
},
testFail: function(name, result) {
result.testName = currentTest;
failedAssertions.push(result);
},
testDone: function(title, state) {
// Log errors if …Run Code Online (Sandbox Code Playgroud) 我收到insert failed: Method not found日志消息,它可能是这些线程中描述的结果:
但是,我没有看到如何.让我展示希望能够更清楚地解释的代码.我正在使用Coffeescript:
if Meteor.isClient
@VINs = new Meteor.Collection("vins")
scoped_vins = @VINs
Template.vins.events =
"click .icon-plus-sign": ->
console.log "this is #{this}"
realVIN = $("#your-vin").val().replace /\D/g, ''
console.log "user id is: #{Meteor.userId()} vin is #{parseInt(realVIN)}"
VINs.insert number: parseInt(realVIN), owner: Meteor.userId() if Meteor.userId()
$("#your-vin").val('')
else
@VINs = new Meteor.Collection("vins")
Run Code Online (Sandbox Code Playgroud)
我对Meteor完全是一个n00b,但我从上面提到的线程中收集的是该集合必须在客户端和服务器上可用.这不是我所做的,还是我发展咖啡失明?
谢谢!
试图测试一些抛出Mocha/Chai异常的代码,但没有运气,这是我试图测试的简单代码:
class window.VisualizationsManager
test: ->
throw(new Error 'Oh no')
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
describe 'VisualizationsManager', ->
it 'does not permit the construction of new instances', ->
manager = new window.VisualizationsManager
chai.expect(manager.test()).to.throw('Oh no')
Run Code Online (Sandbox Code Playgroud)
但是,当我运行规范时,测试失败并抛出异常.
Failure/Error: Oh no
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
我的Web应用程序有两步编译过程.首先,我将CoffeeScript文件编译成JavaScript文件[1].然后,JavaScript grunt-angular-templatesClosure Compiler [2] 将JavaScript文件(来自CoffeeScript的文件和外部的文件,例如由AngularJS模板生成的文件)编译成单个最小化文件.
CoffeeScript ---[1]---> JavaScript --[2]--\
\->
AngularJS templates --> JavaScript ----------> single minimized JS file
/->
other JS files -------/
Run Code Online (Sandbox Code Playgroud)
步骤[1]和[2]都产生源图.
是否可以将这些源映射组合成单个源映射,以允许我从运行最小化JS文件的Web浏览器调试CoffeeScript文件?
换句话说:假设源映射[1]由函数表示:
f(position in CoffeeScript) = position in JavaScript
Run Code Online (Sandbox Code Playgroud)
源映射[2]由函数表示:
g(position in JavaScript) = position in minimized JS
Run Code Online (Sandbox Code Playgroud)
我想得到一个由函数组合表示的源映射:
h(position in CoffeeScript) = g(f(position in CoffeeScript)) =
= position in minimized JS
Run Code Online (Sandbox Code Playgroud) 我试图在Coffeescript中找到一种优雅的方式来合并一个数组,这样[[1,2,3],[4,5,6],[7,8,9]] ==> [1 ,2,3,4,5,6,7,8,9].
正如您可能想象的那样,我需要这个,因为我正在从"for in"构造中的函数生成数组,并且需要连接生成的嵌套数组:
result =(arr中x的generate_array(x))
有一种优雅的方式来处理这个问题吗?感谢您的任何指示!
如何在CoffeeScript中执行以下操作?
$( function() {
$('input#username').keyup( function() {
var username = $('input#username').val();
url = '/users/check_username/';
params = { username : username };
$.get(url, params, function(response){ markUsername(response); }, "json");
});
})
Run Code Online (Sandbox Code Playgroud) 是否可以从Coffeescript中的构造函数中调用方法?
例如
class Animal
constructor: (@name) ->
move()
move: (meters) ->
alert @name + " moved #{meters}m."
class Snake extends Animal
move: ->
alert "Slithering..."
super 5
sam = new Snake "Sammy the Python"
Run Code Online (Sandbox Code Playgroud)
这会生成以下错误消息"ReferenceError:move is not defined"
我的JavaScript如下:
var util = require('util');
EventEmitter = require('events').EventEmitter;
var Ticker = function() {
var self = this;
setInterval( function() {
self.emit('tick');
}, 1000 );
}
Run Code Online (Sandbox Code Playgroud)
什么是等效的CoffeeScript?
我正在尝试完全像在Google地图中那样实现捏合缩放手势.我观看了Stephen Woods的演讲 - "创建响应式HTML5触摸界面" - 关于这个问题,并使用了上面提到的技术.我们的想法是将目标元素的变换原点设置为(0,0)并在此处进行缩放.然后转换图像使其保持在变换点的中心位置.
在我的测试代码缩放工作正常.图像在后续翻译之间放大和缩小.问题是我没有正确计算翻译价值.我正在使用jQuery和Hammer.js进行触摸事件.如何在变换回调中调整计算,使图像在变换点保持居中?
所述的CoffeeScript(#test-resize是一个div与背景图像)
image = $('#test-resize')
hammer = image.hammer ->
prevent_default: true
scale_treshold: 0
width = image.width()
height = image.height()
toX = 0
toY = 0
translateX = 0
translateY = 0
prevScale = 1
scale = 1
hammer.bind 'transformstart', (event) ->
toX = (event.touches[0].x + event.touches[0].x) / 2
toY = (event.touches[1].y + event.touches[1].y) / 2
hammer.bind 'transform', (event) ->
scale = prevScale * event.scale
shiftX = toX * ((image.width() * …Run Code Online (Sandbox Code Playgroud) coffeescript ×10
javascript ×6
jquery ×2
arrays ×1
chai ×1
css3 ×1
gruntjs ×1
include ×1
meteor ×1
mocha.js ×1
node.js ×1
source-maps ×1
touch ×1
unit-testing ×1