[英]In Jade, how can you call a function in an external Javascript
在/javascripts/showlist.js中,我(還有一些未顯示)
var interestMap = gets loaded from localStorage...
function getInterest(id) {
return interestMap[id] || '';
}
我的showlist.jade文件看起來像:(大大簡化了,它在變量“ shows”中的dogshow數組中傳遞)
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()時,我得到“未定義的不是函數”。 因此,它沒有看到它。 我還嘗試了對外部javascript的輕微修改
var getInterest = function(id) { ... }
並將getInterest代碼內聯,但均未成功。
請注意,此擴展的基本布局的第一行具有doctype 5
。
您如何從Jade調用外部(或什至是內部的)JavaScript函數。 還是我錯過了一些簡單而愚蠢的事情? 我也嘗試過“ ../javascripts/showlist.js”。
您正在嘗試在服務器端調用客戶端函數。 這就是為什么它返回為undefined的原因。 它在這里不存在。
您的jade script()
標記只是在頁面上輸出<script>
標記-它不在服務器端運行。 為此,您將改為使用require()
。
由於您指的是localStorage,因此不能簡單地在服務器端復制功能並在其中執行它。
但是,您可以更新showlist.js
以便在dom准備就緒時以其興趣值更新td
。 使用顯示ID將html5數據屬性添加到您的td。 例如。
td(data-show-id=show._id)
然后找到需要更新的td
,並調用getInterest():
$('td[data-show-id]').each(function(index){
var $el = $(this);
$el.val(getInterest($el.data('show-id')));
});
假設您正在運行jQuery。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.