Grasshopper27
Grasshopper27

Reputation: 123

Materialize 1.0.0 gem not allowing jquery functions

With the materialize 1.0.0 gem, I keep getting the "Uncaught TypeError: sideNav" or "dropdown is not a function" and have reverted to the 0.100.2 version of materialize to get things working. I know that this comes from jquery and materialize not working together as they should so if anyone has any insight on this issue I would appreciate it so that I could start using 1.0.0 instead.

Here is some code. Let me know if you need more.

application.js

 //= require rails-ujs
 //= require turbolinks
 //= require activestorage
 //= require jquery
 //= require materialize

application/layouts/shared/_header.html.erb

   <nav>
    <div class="nav-wrapper">
     <a href="#!" class="brand-logo">Logo</a>
     <a href="#" data-target="mobile-demo" class="sidenav-trigger"><i class="material-icons">menu</i></a>
      <ul class="right hide-on-med-and-down">
        <li><a href="sass.html">Sass</a></li>
        <li><a href="badges.html">Components</a></li>
        <li><a href="collapsible.html">Javascript</a></li>
        <li><a href="mobile.html">Mobile</a></li>
      </ul>
    </div>
   </nav>

   <ul class="sidenav" id="mobile-demo">
     <li><a href="sass.html">Sass</a></li>
     <li><a href="badges.html">Components</a></li>
     <li><a href="collapsible.html">Javascript</a></li>
     <li><a href="mobile.html">Mobile</a></li>
   </ul>

   <script>
      $(document).ready(function(){
       $('.sidenav').sidenav();
      });
   </script>

I have also tried reverting to older versions of the jquery-rails gem but other gems in my app rely on at least 4.2 and reverting past this version does not seem to be an option.

Upvotes: 0

Views: 983

Answers (1)

Grasshopper27
Grasshopper27

Reputation: 123

Ok folks, I did a little more research and discovered a post on GitHub that helped me realize that I was wrong about my problem being a version of jquery and that I actually needed to add a coffee script to my assets/javascript folder to help things along. https://github.com/mkhairi/materialize-sass/issues/176

This had never occurred to me before because 0.100.2 never required it. Hopefully this helps anybody who stumbles across this post with the same problem.

Here's the file I added.

assets/javascripts/init.coffee

$(document).on 'turbolinks:before-visit turbolinks:before-cache', ->
  elem = document.querySelector('#slide-out');
  instance = M.Sidenav.getInstance(elem) if elem
 if instance
  instance.close() if instance.isOpen #close on click
  instance.destroy()

 $(document).on 'turbolinks:before-visit turbolinks:before-cache', ->
  $('.toast').remove()

 $(document).on 'turbolinks:load', ->
  elem = document.querySelector('#slide-out');
  instance = new M.Sidenav(elem, {}) if elem

 $(document).on 'turbolinks:load', ->
  Waves.displayEffect()

 # Reset
 M.Modal._count = 0;
 M.ScrollSpy._count = 0;

$('.modal').modal();
$('.carousel').carousel();
$('.collapsible').not('.expandable').collapsible();
$('.collapsible.expandable').collapsible({accordion: false});
$('.tap-target').tapTarget();

$('.slider').slider();
$('.parallax').parallax();
$('.materialboxed').materialbox();

$('.scrollspy').scrollSpy();
$('.datepicker').datepicker();
$('.timepicker').timepicker();
$('.tooltipped').tooltip();
$('.dropdown-trigger').dropdown();

#form
M.updateTextFields()
$('select').not('.disabled').formSelect();

$('input.autocomplete').autocomplete data:
 'Apple': null
 'Microsoft': null
 'Google': 'http://placehold.it/250x250'
$('input[data-length], textarea[data-length]').characterCounter();


 $('.tabs').tabs();
 # Swipeable Tabs Demo Init
 if $('#tabs-swipe-demo').length
  $('#tabs-swipe-demo').tabs 'swipeable': true

 # Chips

 # Handle removal of static chips.
 $(document.body).on 'click', '.chip .close', ->
  $chips = $(this).closest('.chips')
   if $chips.length and $chips[0].M_Chips
  return
  $(this).closest('.chip').remove()

  $('.chips').chips()
  $('.chips-initial').chips
   readOnly: true
  data: [
   { tag: 'Apple' }
   { tag: 'Microsoft' }
   { tag: 'Google' }
  ]
 $('.chips-placeholder').chips
  placeholder: 'Enter a tag'
  secondaryPlaceholder: '+Tag'
 $('.chips-autocomplete').chips autocompleteOptions: data:
  'Apple': null
  'Microsoft': null
  'Google': null

 # Fab
 $('.fixed-action-btn').floatingActionButton()
 $('.fixed-action-btn.horizontal').floatingActionButton direction: 'left'
 $('.fixed-action-btn.click-to-toggle').floatingActionButton
  direction: 'left'
  hoverEnabled: false
 $('.fixed-action-btn.toolbar').floatingActionButton toolbarEnabled: true


 console.log "load init on ready or turbolinks:load"

It's an easy fix. Sidenav and dropdown work great now!

Upvotes: 1

Related Questions