2012-12-18 37 views
7

我想用template.find讓我的生活更輕鬆。流星template.find是undefined

但在JavaScript控制檯我得到:undefined is not a function

這裏是我的。它被絆倒了template.find(...)

Template.superuserHUD.events = 
    { 
    'click input.new_device': function (template) { 
     var id = template.find(".new_device_id").value; 
     Device_IPs.insert({ID: id, IP: "Not Connected"}); 
    } 
    } 

任何想法?

+0

,而不是發現,選擇相匹配的元素,你應該直接檢索元素的值文本使用'.getElementById()'方法 –

+0

嗨user1191551,你已經提出了5個問題,但沒有接受任何答案。在這裏接受你認爲有用的答案是常見的禮節。它還將提高針對未來問題的答案質量。謝謝! – Rahul

+0

謝謝Rahul。看起來,我似乎有2個不同的帳戶,因爲我已經明確回答並接受了比此帳戶顯示的更多答案... – Chet

回答

5

使用第二arugment請像

Template.superuserHUD.events 
    'click input.new_device': (event, template) -> 
     options = 
       id: template.find('.new_device_id').value 
       IP: 'Not Connected' 
     Device_IPs.insert options 

,有時使用模板本身就像

Template.superuserHUD.events 
    event: (event, template) -> 
     @find('.new_device_id').value 

下面是在javascript的咖啡文盲一樣的...

Template.superuserHUD.events({ 
    'click input.new_device': function(event, template) { 
    var options; 
    options = { 
     id: template.find('.new_device_id').value, 
     IP: 'Not Connected' 
    }; 
    return Device_IPs.insert(options); 
    } 
}); 

Template.superuserHUD.events({ 
    event: function(event, template) { 
    return this.find('.new_device_id').value; 
    } 
}); 
+0

抱歉,但我完全不理解此語法。這是JavaScript嗎?我從來沒有見過「 - >」 – Chet

+0

sry它是咖啡的腳本,你可以用它來翻譯http://js2coffee.org/ – crapthings

15

事件處理函數接收兩個參數:event(一個包含關於該事件的信息的對象)和template(定義該處理程序的模板的模板實例)。

第二個參數是可選的,但它需要在處理程序接收時要使用的模板實例功能,如find()findAll()firstNode()lastNode()

因此,使用template.find()在事件處理程序,你需要儘可能傳遞雙方的觀點:

'click input.new_device': function (event, template) { 
    // your event handling code 
    } 
+0

hmm。我試過這個: 'click input.new_device':function(event,template){var。name = this.find(「。new_device_id」)。value; console.log(id) } 但它仍然沒有工作。仍未定義... 並且這是html:ID: \t Chet

+3

你仍然做錯了,在'click input.new_device'中'this'將引用當前的'window'對象,而不是*你正在嘗試使用的''template'。所以,顯然'this.find()'會返回'undefined'。所以,你必須寫''template.find()'而不是'this.find()'。 –

相關問題