abpetkov / switchery

iOS 7 style switches for your checkboxes
http://abpetkov.github.io/switchery/
2.06k stars 479 forks source link

Uncaught TypeError with RequireJS #22

Open fabiocaseri opened 10 years ago

fabiocaseri commented 10 years ago

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

abpetkov commented 10 years ago

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.

infacq commented 10 years ago

looking forward for requireJs

chandra-sekar commented 10 years ago

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.

ingro commented 10 years ago

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

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.

chandra-sekar commented 9 years ago

@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!

charlessolar commented 9 years ago

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

bartzy commented 9 years ago

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

Thanks!

bartzy commented 9 years ago

@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.

jakeorr commented 9 years ago

+1 for requirejs support

vedmant commented 7 years ago

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?

adiploma commented 6 years ago

define() is being invoke multiple times: