0

Is it possible to do something like this:

  Template.hello.rendered = function() {
    $("#timepicker-default").timepicker();
  };

Inside of a package, independent of the actual template name.

Ignoring any other issues with the above pseudocode, what I'm wondering is how can I select DOM elements from within a package. Is it possible to write a package that can find the input element with data-attribute or a class of timepicker and generate the above code inserting the id of that element?

So you could write your markup like

<input id="mytimepicker" type="text" data-timepicker="default">

And the package would handle the rest.

Jeremy Plack
  • 487
  • 1
  • 6
  • 16

1 Answers1

1

I think the fastest solution is to use the template-extension package. You'll get access to an onRendered hook which will fire every time any template in your app is rendered.

$ meteor add aldeed:template-extension

Template.onRendered(function () {
  this.$('input[data-timepicker]').timepicker();
});

Place the above anywhere in your client directory and it should select all all inputs in the template being rendered where the data-timepicker attribute is set.

If you need to use this in a package, just add api.use('aldeed:template-extension'); in your package.js.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • I like this approach, but `this.$(':not([data-timepicker=""])').timepicker();` Seems to select all inputs that don't have the data-timepicker attribute set. – Jeremy Plack Apr 06 '15 at 16:48
  • Try the updated answer. Also see [this question](http://stackoverflow.com/questions/10641258/jquery-select-data-attributes-that-arent-empty). – David Weldon Apr 06 '15 at 17:19
  • These solutions work in a simple fiddle but not here. It looks like the rendered callback is firing twice, not sure if that has anything to do with it. – Jeremy Plack Apr 06 '15 at 18:00
  • the data-attribute is not working out. For now I'm using the template-extension package and class='timepicker'. – Jeremy Plack Apr 06 '15 at 18:04
  • unless there would be a valid reason not to use a class, I'll edit my question and approve this answer. – Jeremy Plack Apr 06 '15 at 18:16