admin管理员组

文章数量:1313749

I've seen many variations to loading modules which do not support AMD yet, and I would like to know what is the best practice to do so.

Eventually, I would like to write modules like this:

module.js:

define(["jQuery", "Underscore", "Backbone"], function($, _, Backbone) {
    ... module code here
}

But there are a lot of problems with loading those dependencies using AMD since they are not all AMD pliant.

I've seen many variations to loading modules which do not support AMD yet, and I would like to know what is the best practice to do so.

Eventually, I would like to write modules like this:

module.js:

define(["jQuery", "Underscore", "Backbone"], function($, _, Backbone) {
    ... module code here
}

But there are a lot of problems with loading those dependencies using AMD since they are not all AMD pliant.

Share Improve this question edited Jan 8, 2012 at 22:34 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Dec 26, 2011 at 8:57 Ron ReiterRon Reiter 3,9343 gold badges32 silver badges34 bronze badges 3
  • 1 What do you mean by AMD? Not the CPU family, right? – Thilo Commented Dec 26, 2011 at 9:02
  • 1 Nope. Asynchronous module definition. Poor choice of TLA I must admit... :) – Ron Reiter Commented Dec 26, 2011 at 9:02
  • 1 A few years back (before the Intel switch, I think), the Mac rumour groups were abuzz with code references to amd. Back then, it was the auto-mount-daemon. ;-) – Thilo Commented Dec 26, 2011 at 9:06
Add a ment  | 

5 Answers 5

Reset to default 4

I've created a todo-list boilerplate web app which loads all modules as AMD modules (without loaders).

Check it out:

https://github./ronreiter/webapp-boilerplate

Thomas Davis has a better example (imo) for loading jquery/underscore/backbone in his non-updated example. Start by looking at the loader here

It uses the RequireJS order plugin found here to load the modules synchronously.

Take a look at this example. It nicely shows how to use backbone along with requirejs. It also shows how you can organize the model, view and collections neatly.

The latest version of RequireJS adds the ability to use NON-AMD JS files.

require.config({ 
    'paths': { 
    "underscore": "libs/underscore-min", 
        "backbone": "libs/backbone-min"
    },
    'shim': 
    {
        backbone: {
            'deps': ['jquery', 'underscore'],
            'exports': 'Backbone'
        }
    }   
});

Give that a try.

One thing i don't understand about AMD is that looks like it loads necessary js only when it needs but with the demo application it loads all js html css files when you access the app for the first page load.

本文标签: