Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

AMD support #4

Closed
timmywil opened this issue Sep 27, 2013 · 7 comments
Closed

AMD support #4

timmywil opened this issue Sep 27, 2013 · 7 comments
Assignees

Comments

@timmywil
Copy link

It would be great if this plugin didn't assume a jQuery global. This could be done with the UMD syntax for jQuery plugins.

@tylersticka
Copy link
Member

@timmywil That's a great idea!

@ghost ghost assigned tylersticka Sep 27, 2013
@tylersticka
Copy link
Member

Hey @timmywil, maybe you can help me with something... I'm a designer first and a developer second, so it's hard for me to judge the best way to tackle this from a developer perspective... especially since I've never used RequireJS before!

hideShowPassword works with either jQuery or Zepto. This is easy enough in the current version of the plugin, as I can just pass this.jQuery || this.Zepto for the $ argument and call it a day. But in the case of defining dependencies, it looks like I have to be a bit more overt.

This post suggests doing something like this:

define(('__proto__' in {} ? ['zepto'] : ['jquery']), function($) {
    return $;
});

This makes sense to me if your library overtly recommends the use of both Zepto and jQuery, but my goal in writing the plugin that way was flexibility... I would guess most users will be favoring jQuery, so it seems a pretty big assumption to say "when '__proto__' in {} returns something truthy, always use Zepto."

I apologize if this is a newb question, but how would you recommend supporting both frameworks?

@timmywil
Copy link
Author

No problem! I'd recommend something like this:

(function (factory, global) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        // To use Zepto, map Zepto to the the name 'jquery' in your paths config
        define(['jquery'], factory);
    } else {
        // Browser globals
        factory(global.jQuery || global.Zepto);
    }
}(function ($) {
    // hideShowPassword code here
}, this));

AMD folks who use Zepto are used to mapping zepto already because Zepto doesn't register as an AMD module (much to the objection of Zepto's users and the front-end community). I would simply document that using zepto instead is a matter of path config:

require.config({
  paths: {
    jquery: 'path/to/zepto'
  }
});

This will map Zepto to the name 'jquery' and the define call will pull in Zepto for the $ variable instead.

@tylersticka
Copy link
Member

@timmywil Perfect. Thanks so much for the detailed explanation... knowledge is awesome. :)

@tylersticka
Copy link
Member

@timmywil Okay, I think I have this working... but just in case, I pushed to an amd-support branch.

If you get a moment, would you mind taking a look or giving it a whirl before I unleash it on an unsuspecting populace?

@timmywil
Copy link
Author

👍 I just tested and it works great! Thanks!

@tylersticka
Copy link
Member

@timmywil Sweet! Branch merged, release tagged, and bob's your uncle. Thanks again for your help.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants