在翡翠,你怎么能在一个外部的Javascript调用一个函数

在/javascripts/showlist.js我有(加一些更多没有显示)

var interestMap = gets loaded from localStorage... function getInterest(id) { return interestMap[id] || ''; } 

我的showlist.jade文件看起来像:(大大简化,它被传递给variables“shows”中的一个dogshows数组)

 extends layout script(src='/javascripts/showlist.js') block content table#showtable.sortable thead tr td... (column headers such as date and location, many are sortable) tbody each show in shows - var interest = getInterest(show._id); <<< JADE problem here tr td... (various values, including) td =interest // maybe this should be #{interest} 

但是当我尝试调用getInterest()时,我得到“undefined不是一个函数”。 所以没有看到它。 我也试过了对外部javascript的一个轻微的mod

 var getInterest = function(id) { ... } 

并把getInterest代码内联,既没有成功。

请注意,这是扩展的基本布局有doctype 5因为它的第一行。

你如何从Jade调用外部(或者,我甚至会解决一个内部的)javascript函数。 还是我缺less一些简单而愚蠢的东西? 我也尝试了“../javascripts/showlist.js”。

您正试图在服务器端调用客户端function。 这就是为什么它回到未定义。 这里不存在。

你的jade script()标签只是输出一个<script>标签到页面 – 它不运行在服务器端。 要做到这一点,你需要使用require()

既然你指的是localStorage,你不能简单地复制服务器端的function,并在那里执行它。

但是,您可以更新您的showlist.js以便在准备就绪时更新td与他们的兴趣值。 添加一个HTML5数据属性到您的td与展示ID。 例如。

 td(data-show-id=show._id) 

然后find需要更新的td ,并调用getInterest():

 $('td[data-show-id]').each(function(index){ var $el = $(this); $el.val(getInterest($el.data('show-id'))); }); 

假设你正在运行jQuery。