javascript - Where should I initialize select when using Materialize and Meteor? - Stack Overflow

I'm trying to use Materialize Forms on Meteor. On its Materialize's page it says I should ini

I'm trying to use Materialize Forms on Meteor. On its Materialize's page it says I should init the "select" input field like this:

$(document).ready(function() {
  $('select').material_select();
});

I've tried calling this on Meteor.startup, Template.body.created - nothing worked. I get the following error:

undefined is not a function (evaluating '$('select').material_select()')

Where should I initialize it?

I'm trying to use Materialize Forms on Meteor. On its Materialize's page it says I should init the "select" input field like this:

$(document).ready(function() {
  $('select').material_select();
});

I've tried calling this on Meteor.startup, Template.body.created - nothing worked. I get the following error:

undefined is not a function (evaluating '$('select').material_select()')

Where should I initialize it?

Share Improve this question asked Feb 28, 2015 at 16:16 Aviad Ben DovAviad Ben Dov 6,4092 gold badges35 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Use the template's .rendered callback

<template name="hello">
    <select><option>...</option></select>
</template>

Then you can have this in your js file

Template.hello.onRendered(function() {
    $('select').material_select();
});

The template is added to the body most likely after rendered has already fired so thats why the body rendered didn't work. If you use .created the DOM hasn't rendered yet.

Akshat's answer is correct but if it still doesn't work for you, there might be a problem with subscription not fired yet or not every needed part of DOM beeing rendered. Use afterflush then.

Template.listing.onRendered(function () {
    var template = this;

    template.subscribe('listOfThings', function () {
    Tracker.afterFlush(function() {
       template.$('select').material_select();
    });
  });
});

Here is a conversation about that: https://github./meteor/meteor/issues/4401#issuement-103340262

And the docs: http://docs.meteor./api/tracker.html#Tracker-flush

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745454394a4628418.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信