【问题标题】:zappa js @include coffeescript method doesn't workzappa js @include 咖啡脚本方法不起作用
【发布时间】:2013-02-15 21:37:06
【问题描述】:
我希望能够从 zappa 应用程序调用在其他文件中声明的咖啡脚本和 js 函数。我根本无法让它工作。
我尝试按照 zappajs crashcourse 中的说明使用 @include ...
但我明白了
TypeError: Object # has no method 'include'
这是我的测试应用代码:
#app.coffee
require('zappajs') ->
@get '/': -> @include 'call'
这是我试图在另一个文件中调用的函数。
#call.coffee
@include = ->
"call me"
【问题讨论】:
标签:
node.js
function
include
coffeescript
zappa
【解决方案1】:
尚未测试任何内容,但您似乎在这里混淆了一些东西。我可能是错的,但你可能应该只使用 require node.js-style require,或者你可以使用 zappa style @include,但是在你真正理解 @987654325 之前混合它们可能不是一个好主意@ 会。
您链接到的 zappa 速成课程显示了定义模块和使用它们,但这两个地方都是使用 @include 完成的。根据你写的,我相信你不能像你试图做的那样混合require和@include。
【解决方案2】:
Marius 是对的,我把 require 和 @include 混在一起了……我可以让它工作,这正是我想要做的(即在另一个文件中调用函数)
require('zappajs') ->
test = require('./test')
@get '/': -> test.test(@response)
有一个名为 test.coffee 的文件,看起来像这样
@test = (res) ->
res.send 'hullo'
【解决方案3】:
尝试:
#app.coffee
require('zappajs').run port, host, ->
@include './routes'
#routes.coffee
@include = ->
@get '/': ->
@render 'index.jade',
foo:'bar'