switchery icon indicating copy to clipboard operation
switchery copied to clipboard

Uncaught TypeError with RequireJS

Open fabiocaseri opened this issue 11 years ago • 11 comments

Switchery is not working for me when using RequireJS (v2.1.10). It seems the problem is present since the introduction of FastClick library. In console I get "Uncaught TypeError: object is not a function" at line 1311, doing some debugging I see that fastclick var is an empty object.

I've uploaded an example: Preview: http://bl.ocks.org/fabiocaseri/8956655

Gist: https://gist.github.com/fabiocaseri/8956655

fabiocaseri avatar Feb 12 '14 17:02 fabiocaseri

It appears when using requireJS, module.exports is not present (this if statement) and therefore an empty FastClick object is returned.

It seems that FastClick is not maintained very well, seeing their issues and pull requests, so I won't bother to fix that for them. I'll find an alternative to use and replace this dependency. Until then, apply some temporary fix so that the FastClick.attach method is attached to module.exports and watch for further update on this issue from me.

abpetkov avatar Feb 13 '14 08:02 abpetkov

looking forward for requireJs

infacq avatar Jun 26 '14 09:06 infacq

Hi, any updates on this? Some more info on how to apply the temporary fix will be very useful. I am trying to use this on a backbone project. Thanks for the library.

chandra-sekar avatar Aug 04 '14 05:08 chandra-sekar

@chandra-sekar A quick fix for the problem would be:

  • go to switchery.js file in the dist folder and search for FastClick.attach function.

Few lines below, change this if statement from

if (typeof define !== 'undefined' && define.amd) {

    // AMD. Register as an anonymous module.
    define(function() {
        'use strict';
        return FastClick;
    });
}

to

if (typeof define !== 'undefined' && define.amd) {

        // AMD. Register as an anonymous module.
    module.exports = FastClick.attach;
    module.exports.FastClick = FastClick;
}

Not the prettier solution but it works.

ingro avatar Sep 04 '14 14:09 ingro

@ingro I somehow missed your reply. I had reverted to using an old version of switchery (pre-fastclick). I have now switched over to the new version using your fix. Thanks a lot!

chandra-sekar avatar Oct 16 '14 08:10 chandra-sekar

+1 for a permanent fix, editing the file all the time is a pain

charlessolar avatar Mar 10 '15 02:03 charlessolar

@abpetkov , Do you plan to fix this issue? Without the change proposed by @ingro, Switchery cannot be used with RequireJS.

Thanks!

bartzy avatar Mar 10 '15 11:03 bartzy

@abpetkov After some further researched, I don't think this is fastclick's problem. For example, Hammer.js, which is well maintained, is behaving in the same wayif you include them as a ComponentJS dependency.

What happens is the following: Somewhere in a library (such as Fastclick or Hammer.js for example), there is something like this:

if (typeof define !== 'undefined' && define.amd) {
    // AMD. Register as an anonymous module.
    define(function() {
        'use strict';
        return FastClick;
    });
} else if (typeof module !== 'undefined' && module.exports) {
    module.exports = FastClick.attach;
    module.exports.FastClick = FastClick;
} else {
    window.FastClick = FastClick;
}

When your code uses FastClick as a Component module, it concatenates this code as part of the FastClick module. When this code runs in a RequireJS environment, it defines a RequireJS anonymous module, but the file name is switchery.js and not related to fastclick, so RequireJS gets totally confused.

Do you have any direction on how to approach fixing this? I know this is not directly related to Switchery.

bartzy avatar Mar 11 '15 15:03 bartzy

+1 for requirejs support

jakeorr avatar Apr 28 '15 18:04 jakeorr

It looks like this problem is still there, it's just not working with Webpack, still no permanent solution without editing packages file for this?

vedmant avatar Apr 19 '17 15:04 vedmant

define() is being invoke multiple times:

  • the first one is to define the Switchery
  • the second is to define the FastClick

adiploma avatar Sep 12 '18 07:09 adiploma