我在article中读到了我可以使用内联函数来定义它们 - var = function(...),但是当我编写代码时:
- var tryit = function tryit(tasks) {alert('will not work')};
if tasks
#adiv #{tryit(tasks)}
我收到此错误消息:
if supervisor tryit is not a function
我扫描了所有的哈巴狗文档(当然没有那样的东西,当然!),请告诉我如何使用这种方法,以便我可以进一步处理从客户端服务器发送的变量'tasks' .
我的用例是,如果我有一个来自服务器的json数组,进一步处理它并添加DOM元素的树结构 . 然后最后将它附加到div(id = 'adiv') . 我可以't anyway figure out how to do this, because when I use document.createElement(...) I get a similar error like the above with alert(..). I tried by script. but then I can' t调用我在脚本中声明的方法 . 在pug模板中使用#{myScriptedFunc(...)} .
Edit:
令我更惊讶的是:如果我将其更改为:
- var tryit = function() {console.log('silence!');};
if tasks
#adiv #{tryit()}
将没有错误消息,但输出将在服务器端(在我运行服务器的命令行窗口中),而不是在浏览器中,这意味着该行是在服务器端执行的?但我希望它能在客户端运行 . 在客户端进行这样的后处理的方式对我来说仍然是一个问题 .
2 回答
你的变量应该保存函数的'name',函数本身应该是匿名的 .
所以
var tryit = function tryit(tasks)
变成var tryit = function(tasks)
警报可以工作,它看起来不太好,但它在这里:
if messages !== null each message in messages mixin alertMessage(message) script. debugger; alert(message); +alertMessage(message) else mixin alertMessage() script. debugger; alert('Hey Look i work!'); +alertMessage()
使用 mixin 将允许您调用类似于您可以放置普通javascript的代码块的函数 .
我想你可以做if检查,然后把它放进去
标签之后,但是
将允许您遍历您在路径中发送的数组或对象