Backbone fine(['jquery', 'jqueryui', 'lodash', 'backbone'], function($, null, _, Backbone) /* This function gets called after jquery.js, jqueryui.js, lodash.js, and backbone.js are loaded */ ;
Can return any type of value (e.g object, function, string) and defaults to undefined if no return value is specified. The module id and dependencies arguments are optional // example.js // Does not depend on any files and returns a string define(function() { return 'No dependencies, returns a string'; });
Example // getUser.js // Depends on a file (users.js), and returns a getUser function define(['./users'], function(users) { return function getUser(username) { return users[username]; }; });
the example.js module from our first define() example require(['./example.js'], function(exampleText) { // Prints: 'example.js return value: No dependencies, returns a string' console.log('example.js return value: ', exampleText); }) That's All There Is To The require() Method
Use define to wrap logic that you would like to re-use somewhere else in your application Use require when you just want a dependency to be loaded onto the page.
Conditionally Wrapping Itself in a define method // If an AMD compatible loader is on the page if ( typeof define === "function" && define.amd ) { define([], function() { return exampleLib; }); }
wrapper for all shimmed libraries define("backbone", ["underscore","jquery"], (function (global) { return function () { var ret, fn; return ret || global.Backbone; }; }(this)));
used for all of the file lookups paths Allows you to map module IDs to alias names (easier to type 'jquery' than '../../jquery') shim Allows you to make third-party libraries AMD compatible
Step? It doesn't, the optimizer is optional. Why Would I use the RequireJS Optimizer? If you want to build your project into one file Do I Still Need RequireJS After I Build? If you need to load any additional network resources with RequireJS, then yes. Otherwise, you may use Almond.js or AMDClean.js.
Standard JavaScript Allows You To Use AMD In Development Without Having To Include an AMD Library In Production Does Not Allow Loading Network Resources Transforms Source Code Example Build With AMDClean.js