我有以下代码,我想避免嵌套回调:
app.get '/performers', (req, res) ->
conductor = require('models/conductor').init().model
soloist = require('models/soloist').init().model
orchestra = require('models/orchestra').init().model
chamber = require('models/chamber').init().model
performers = {}
conductor.find {}, (err, result) ->
performers.conductor = result
soloist.find {}, (err, result) ->
performers.soloist = result
orchestra.find {}, (err, result) ->
performers.orchestra = result
chamber.find {}, (err, result) ->
performers.chamber = result
res.json performers
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
可以像这样格式化字符串:
text = 'text'
formatted = "Text: #{text}"
Run Code Online (Sandbox Code Playgroud)
以下怎么样?
pattern = "Text: #{text}"
text = 'text'
formatted = ???
Run Code Online (Sandbox Code Playgroud) 我在第一个Rails应用程序中遇到了CoffeeScript问题.我正在使用waitForImages jQuery插件,该插件保存在一个名为的单独文件中waitforimages.jquery.js.Rails自动创建home.js.coffee,我想在其中包含以下jQuery代码段:
$('#fullbleed').waitForImages(function() {
$(this).fadeIn(3000);
});
Run Code Online (Sandbox Code Playgroud)
但是我怎么用CoffeeScript表示法写这个呢?
UPDATE
现在情况很好,所以我想我会发布我的最终代码.其中一个问题是我没有在 home.js.coffee 之前加载waitForImages插件.
CoffeeScript的:
$(document).ready -> $('#fullbleed').waitForImages -> $(@).fadeIn 3000
Run Code Online (Sandbox Code Playgroud)
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.waitforimages.js?body=1" type="text/javascript"></script>
<script src="/assets/home.js?body=1" type="text/javascript"></script>
</head>
<body>
...
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我在coffeescript中使用while循环时遇到了一些麻烦.
原始功能如下,来源是这个答案:
// Return all pattern matches with captured groups
RegExp.prototype.execAll = function(string) {
var match = null;
var matches = new Array();
while (match = this.exec(string)) {
var matchArray = [];
for (i in match) {
if (parseInt(i) == i) {
matchArray.push(match[i]);
}
}
matches.push(matchArray);
}
return matches;
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作.我通过js2coffee转换它,咖啡脚本是:
# Return all pattern matches with captured groups
RegExp::execAll = (string) ->
matches = new Array()
match = null
while match = @exec(string)
matchArray = …Run Code Online (Sandbox Code Playgroud) 我是Grunt/Yeoman的新手,我有一个现有的应用程序,有40多个Coffeescript文件,如下所示:
scripts/
lib/
...
common/
...
util/
...
app/
views/
models/
A.cofffee
B.cofffee
C.cofffee
Run Code Online (Sandbox Code Playgroud)
我想以特定的顺序连接它们并编译成一个文件.
所以我想说"按此顺序编译"
scripts/lib/some_superclass.coffee
scripts/lib/*
common/*
util/app/views/*
util/app/models/some_model_that_needs_to_be_required_first.coffee
util/app/models/*
Run Code Online (Sandbox Code Playgroud)
Grunt/Yeoman项目如何解决这个问题?(我真的不想拼出每个文件)
我无法将函数返回值赋给变量.当我将其记录到控制台时,为什么我会返回函数而不是最终产品?
time = ->
today = new Date()
minutes = today.getMinutes()
if minutes < 10 then minutes = "0#{minutes}"
hours = today.getHours()
if hours < 10 then hours = "0#{hours}"
"#{hours}:#{minutes}"
console.log time
Run Code Online (Sandbox Code Playgroud) 我正在使用remotipart使用ajax上传和升级图像,问题是当我编辑项目时,ajax更新数据,但remotipart(https://github.com/leppert/remotipart)返回图像的'解析错误'更新.
这就是我的表单的样子:
= form_for(Achievement.new), html: {multipart: true , remote: true} do |f|
= f.text_field :name
= f.text_area :description
= f.file_field :image
= f.submit 'Send'
Run Code Online (Sandbox Code Playgroud)
我正在使用单一表单来创建,编辑和删除"成就".这是我的js:
constructor: ->
$('.edit_button').click ->
$.ajaxSettings.dataType = "json"
@id = $(this).data('id')
@content = $(this).parent()
@name = $('.form_achievement #name')
@description = $('.form_achievement #description')
@image = $('.form_achievement .avatar img')
@button = $('.form_achievement form input:submit')
@form = $('.form_achievement form')
#Load data to edit on form
$.ajax
type: 'get'
url: "/en/private/achievements/#{@id}/edit/"
success: (data) =>
alert 'edit'
@name.val(data.achievement.name)
@description.val(data.achievement.description)
@stat.html(data.achievement.stat)
@value.val(data.achievement.value) …Run Code Online (Sandbox Code Playgroud) 这是我的注销点击事件:
logoutClicked: (event) ->
event.preventDefault()
console.log 'userPanel.logoutClicked -> event', event
console.info App.session
App.session.destroy
wait: true
success: (model, res) ->
console.log 'session.destroy.success -> model/res', model, res
error: (model, res) ->
console.log 'session.destroy.error -> model/res', model, res
Run Code Online (Sandbox Code Playgroud)
这是我的会话模型:
class App.Model.Session extends Backbone.Model
initialize: ->
console.log 'Session.init'
urlRoot: '/session'
Run Code Online (Sandbox Code Playgroud)
这是我纤细的后端路线:
$app->delete('/session', function () {
session_unset();
exit(true);
});
Run Code Online (Sandbox Code Playgroud)
当我触发logoutClicked事件时,一切正常,但我无法通过我的firebug看到任何服务器通信(没有DELETE或GET到/ session ...).
Firebug输出:
userPanel.logoutClicked -> event Object { originalEvent=Event click, type="click", timeStamp=18807379, altri elementi...}
Session { cid="c1", attributes={...}, _changing=false, altri elementi...}
session.destroy.success -> model/res Session …Run Code Online (Sandbox Code Playgroud) 当我尝试转换以下代码片段时......
result.pause = function() {
cachedValue = this();
isPaused(true);
}.bind(result);
Run Code Online (Sandbox Code Playgroud)
result.pause = ->
cachedValue = this()
isPaused true
.bind(result)
Run Code Online (Sandbox Code Playgroud)
但是,当您尝试编译它时,该代码是不正确的,您将返回Error Unexpected'.'
使用CoffeeScript在这种情况下使用.bind函数的正确方法是什么?
if myString then myString else ""
Run Code Online (Sandbox Code Playgroud)
......感觉有点冗长.
我可以使用更短的替代品吗?
myString 可以是undefined或字符串.
coffeescript ×10
javascript ×4
jquery ×2
ajax ×1
asynchronous ×1
backbone.js ×1
express ×1
gruntjs ×1
mongoose ×1
node.js ×1
promise ×1
regex ×1
rest ×1
scope ×1
slim ×1
yeoman ×1