Lasang
Lasang

Reputation: 1379

using require.js to load javascript libraries

I am trying to have folder structure given here.

The difference is I am using all latest development version of these libraries. However, they are not correctly being uploaded, even jquery. I could not understand what by AMD compatible mean.

I simply downloaded those libraries. Does it mean they are not AMD compatible? Is there a separate AMD compatible version of those libraries?. Do we need to use shim config to load non AMD compatible?.

I am afraid to use shim because require.js official documentation says it is not recommended.

Upvotes: 0

Views: 181

Answers (2)

c24w
c24w

Reputation: 7856

@kryger's right that jQuery is AMD-compatible and there's nothing wrong with shimming non-AMD modules.

AMD-compatible just means that the module adheres to the AMD specification; hence it exports its functionality. For example:

define(['jquery', 'underscore'], function ($, _) {
    // function body
});

So in the function body, above:

  • you can access jQuery via the $ argument; but
  • underscore doesn't export itself for AMD, so the _ argument will be undefined.

Some AMD-compatible modules (e.g. jQuery) also work 'standalone', such as in an HTML <script> tag, where they will export to the global namespace (e.g. window.$).

I think jQuery still exports the global $ when you load it via an AMD loader, so you could just load jQuery once at your entry-point / when it is first needed and always access the global $ (rather than defining a $ argument).

Also, if you use jQuery plugins (which modify the global one) it can all get a bit confusing!

Upvotes: 2

kryger
kryger

Reputation: 13181

I couldn't find any mention of shim being "not recommended" in the documentation; I wouldn't be too worried about it. It's a well-tested and increasingly popular way of bridging non-AMD-compliant libraries with RequireJS (previously the only way to do this was was to build a dedicated AMD version wrapped in the define call; that puts extra effort on the library maintainers).

If by "latest development version" you mean jQuery's Pre-Release Builds, it looks like it does support AMD (have a look at the pre-release section of the download page, scroll down to very bottom the source code file: you'll see code relating to AMD)

Backbone and underscore need to be shimmed in any case since they are AMD-agnostic, so there's no escaping from it (these two particular libraries are even used in shim config option documentation example, so you can just reuse that).

Upvotes: 1

Related Questions