Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor template.find is undefined

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?

like image 750
Chet Avatar asked Dec 18 '12 10:12

Chet


2 Answers

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
  }
like image 144
sohel khalifa Avatar answered Oct 23 '22 22:10

sohel khalifa


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;
  }
});
like image 45
crapthings Avatar answered Oct 24 '22 00:10

crapthings