Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Use AMD loading with fallback to global vars#59

Closed
kennknowles wants to merge 1 commit intocarhartl:masterfrom
kennknowles:master
Closed

Use AMD loading with fallback to global vars#59
kennknowles wants to merge 1 commit intocarhartl:masterfrom
kennknowles:master

Conversation

@kennknowles
Copy link

Hello,

I'm porting a project to use RequireJS so I added AMD support to this jquery plugin that I've been using. The change is unobtrusive and backwards compatible, so I hope you'll consider bringing it into the mainline.

I got the basic pattern from here: https://github.com/umdjs/umd/blob/master/jqueryPlugin.js

  • Kenn

@brandonaaron
Copy link

Might want to add && define.amd.jQuery to the conditional as jQuery itself does.

@jpillora
Copy link

Require.js shim config is your friend.

Here's an example from one of my projects:

    shim: {
      'backbone': {
        deps: ['underscore', 'jquery'],
        exports: 'Backbone'
      },
      'bootstrap': ['jquery'],
      'lib/jquery.cookie': ['jquery'],
      'lib/jquery.color': ['jquery']
    }

@kennknowles
Copy link
Author

Indeed, I am using a shim now. This patch would remove the need for a shim.

@jeffreymb
Copy link

I'll add my voice to the usefulness of this addition. Being AMD compatible would allow me to load it from cdnjs.com instead of having to compile it in.

@ghost ghost assigned carhartl Jan 26, 2013
@carhartl
Copy link
Owner

Thanks for the PR! Will add soon.

@carhartl
Copy link
Owner

@brandonaaron Out of interest, what is the effect of the additional check?

@carhartl carhartl closed this in fb6aa59 Jan 26, 2013
@pboling
Copy link

pboling commented Jan 26, 2013

Excellent!

@chas-ryder
Copy link

It appears that this commit added support for requirejs, but it created issues with requirejs build. When running requirejs build, I get a jquery.cookie is undefined error at runtime.

@pboling
Copy link

pboling commented Aug 26, 2013

Now that it is registering as an AMD module you likely need to add it as a dependency to your other scripts where it is used, if you are using RequireJS.

@chas-ryder
Copy link

That's what I am doing. The module is in fact the requirejs build code so the dependency is working, but it is having issue instantiating it. This is only when you do a compile/optimization. It works fine outside of that.

@pboling
Copy link

pboling commented Aug 26, 2013

Are you loading it from a CDN? If so, and if it works when not compiled, then it might be a problem with the way r.js compiled external library dependencies. It has broken several times recently. Also if you are using almond.js to compile, then you cannot load it as an external dependency.

@chas-ryder
Copy link

Nope. It's local. This is the only plugin having issues. I am using a lot of other jquery plugins as well with no issue.

@pboling
Copy link

pboling commented Aug 27, 2013

I wish I could be of more help then. I may attempt to use this plugin again soon, but am not currently.

@davyboyhayes
Copy link

I have come up against this problem, r.js is compiling the code incorrectly:

My shim:

shim: {
...
        jqueryCookie: ['jquery'],
...
    }

And the source currently shows

 if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery'], factory);

But when it is run through r.js, it becomes:

 if (typeof define === 'function' && define.amd) {
        // AMD
        define('jqueryCookie',['jquery'], factory);

@davyboyhayes
Copy link

@chas-ryder I've created #306 as I've come across this issue as well.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants