materialize: Bug when used with AMD (require.js)
the company i am working with likes material design, so i told them about materialize, and they said ok use it , no problem , but currently i am stuck with this so please help !
when i try to require materialize , no errors are shown, but when i actually use it with jquery, it tells me that the method that i am trying to use on that element is undefined here is my code (js):
requirejs.config({
paths: {
'materialize': '../../bower_components/materialize/dist/js/materialize',
'jquery': '../../bower_components/jquery/dist/jquery'
},
shim: {
'materialize': {
deps: ['jquery']
},
'jquery': {
exports: '$'
}
}
});
require(['jquery', 'materialize'], function($) {
$('select').material_select();
});
Screen Shot:
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 96 (7 by maintainers)
Commits related to this issue
- add backup code for other unusal cases of usage, closed #634 — committed to max1011/materialize by max1011 9 years ago
- feat(amd): build materialize-css amd with script npm run mamd https://github.com/Dogfalo/materialize/issues/634#issuecomment-113213629 — committed to Ullfis/aurelia-materialize-bridge by Ullfis 8 years ago
@lmadrilejos How about my solution? If it’s hard for you to modify them, check out my forked and modified version: https://github.com/ccloli/materialize/tree/AMD-fix/dist/js
@ZiperRom1 I’ve noticed that Materialize was updated earlier today, but then I almost forgot it, sorry for not updating in time, I’ll updated it ASAP (about 2 hours later) 😃 AFAIK, I guess the reason why Materialize doesn’t support AMD is that some libraries like hammer has already assigned itself as AMD modules, and Materialize concat all the libraries into one single materialize.js. So if we use Materialize as AMD module directly, it may have errors. So here are two solutions: assign all the libraries as single AMD modules (max1011 solution); remove the AMD assigned code of each libraries (my solution). Hope this can help you.😃
@ccloli HI, thanks very much, I tried doing it earlier today and it works now, thanks for the solution 😃, you’re a life saver. thanks again… 😃
I’m using the latest Materialize (0.97.5) with curl.js (AMD), then I tried using
$(...).sideNav()
and occurred an errordragTarget.hammer is not a function
.Luckily, I deleted some lines of
define
and it works fine now. Here are my steps (with offical 0.97.5, not the unoffical-AMD version):(Notice that on minify version, some variables will probably be modified)
1 . Delete this part at Line 232:
2 . Delete this part at Line 234:
3 . Delete Line 235~239, 241:
4 . Add this part to show waves (see https://github.com/Dogfalo/materialize/issues/634#issuecomment-154833830) :
Update on Mar 13, 2016: I’m sorry that some parts of code is copied from
materialize.min.js
, and their variables are not the same as unminified version. Now they have been fixed.I am also running into similar issues when trying to load materialize. I tried to follow the suggestions above but in the version that I downloaded from the Materialize site (v0.95.3) it seems as though Hammer, Velocity, Scrollfire, etc… have been merged into a single materialize.js file so I don’t think that I can reference them independently in requireJS definition. I am seeing the following errors when attempting to use materialize and also noticing that the UI loads but does not fully function (JS based features are not working).
Errors
My RequireJS Definition File