最近开始学习CoffeeScript,遇到了这样的问题。我想写 javascript :
TemplateManager.tmpl(this.template, this.modelJSON(), this.templateOptions()).done(
function(rendered) { // something1
}).fail(function(ex) {
// something2
});
我可以通过哪种方式获得它?我尝试重写:
TemplateManager.tmpl @template, @modelJSON(), @templateOptions()
.done (rendered) ->
#something1
.fail (ex) ->
#something2
我得到:
TemplateManager.tmpl(this.template, this.modelJSON(), this.templateOptions().done(function(rendered) {
}).fail(function(ex) {
}));
最佳答案
为tmpl
和done
方法添加括号
TemplateManager.tmpl( @template, @modelJSON(), @templateOptions() )
.done( (rendered) ->
#something1
)
.fail (ex) ->
#something2
解决方案并不优雅,我认为其他人可能会在 coffeescript 中提供更好的方法
已更新
根据评论,删除done
的括号。我已经更新了代码,我认为这个代码很优雅
TemplateManager
.tmpl(@template, @modelJSON(), @templateOptions())
.done (rendered) ->
some
code
here
.fail (ex) ->
another
code
here
关于javascript - Coffeescript - 方法链与函数参数 [2] 或使用函数而不是参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15494410/