2012-12-18 42 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()'。 –

相关问题