最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor template.find is undefined - Stack Overflow

programmeradmin4浏览0评论

I am trying to use template.find to make my life easier.

But in the javascript console I get: undefined is not a function

Here's what I have. It is getting tripped up on 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"});
    }
  }

Any ideas?

I am trying to use template.find to make my life easier.

But in the javascript console I get: undefined is not a function

Here's what I have. It is getting tripped up on 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"});
    }
  }

Any ideas?

Share Improve this question edited Jun 4, 2013 at 0:06 Jean-Bernard Pellerin 12.7k10 gold badges58 silver badges82 bronze badges asked Dec 18, 2012 at 10:42 ChetChet 19.8k15 gold badges76 silver badges118 bronze badges 3
  • instead of finding the element that matches the selector, you should directly retrieve the value-text of element using .getElementById() method – sohel khalifa Commented Dec 18, 2012 at 10:51
  • Hi user1191551, you've asked 5 questions but not accepted any answers to them. It's considered common courtesy here to accept answers you find useful. It will also improve the quality of answers given to your future questions. Thanks! – Rahul Commented Dec 26, 2012 at 1:01
  • thanks Rahul. It appears that I seem to have 2 different accounts because I have definitely answered and accepted more answers than this account shows... – Chet Commented Jan 4, 2013 at 20:13
Add a comment  | 

2 Answers 2

Reset to default 16

The Event handler function receives two arguments: event, an object with information about the event, and template, a template instance for the template where the handler is defined.

The second parameter is optional, but it needs to be received in handler when you want to use template instance functions like find(), findAll(), firstNode() and lastNode().

So, to use the template.find() in your event handler, you need to pass both the arguments as:

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

use a second arugment please like

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

and sometimes use template itself like

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

Here's the same in javascript for the coffee illiterate...

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)

  1. 暂无评论