Meteor template.find未定义

Che*_*het 8 javascript events templates meteor

我想用来template.find让我的生活更轻松.

但是在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"});
    }
  }
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

soh*_*ifa 16

事件处理程序函数接收两个参数:event一个包含事件信息的对象,以及template定义处理程序的模板的模板实例.

第二个参数是可选的,但它需要在处理程序接收时要使用像模板实例功能find(),findAll(),firstNode()lastNode().

因此,要template.find()在事件处理程序中使用,您需要将两个参数都传递为:

'click input.new_device': function (event, template) {
     // your event handling code
  }
Run Code Online (Sandbox Code Playgroud)

  • 你仍然做错了,在''click input.new_device'中``this`将引用当前的`window`对象,*不是*你试图使用的`template`.所以,很明显`this.find()`将返回`undefined`.所以,你必须写*`template.find()`而不是`this.find()`. (3认同)

cra*_*ngs 5

请使用第二个安慰

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

有时使用模板本身

Template.superuserHUD.events
    event: (event, template) ->
        @find('.new_device_id').value
Run Code Online (Sandbox Code Playgroud)

这里的咖啡文盲的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);
  }
});
Run Code Online (Sandbox Code Playgroud)
Template.superuserHUD.events({
  event: function(event, template) {
    return this.find('.new_device_id').value;
  }
});
Run Code Online (Sandbox Code Playgroud)