Skip to content

Instantly share code, notes, and snippets.

@caot
Created October 8, 2019 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caot/39d3094716fe0d8d3803e939aa66a2bc to your computer and use it in GitHub Desktop.
Save caot/39d3094716fe0d8d3803e939aa66a2bc to your computer and use it in GitHub Desktop.
// shim: Configure the dependencies, exports, and custom initialization
// for older, traditional "browser globals" scripts that do not use
// define() to declare the dependencies and set a module value.
requirejs.config({
//Remember: only use shim config for non-AMD scripts,
//scripts that do not already call define(). The shim
//config will not work correctly if used on AMD scripts,
//in particular, the exports and init config will not
//be triggered, and the deps config will be confusing
//for those cases.
shim: {
'backbone': {
//These script dependencies should be loaded before loading
//backbone.js
deps: ['underscore', 'jquery'],
//Once loaded, use the global 'Backbone' as the
//module value.
exports: 'Backbone'
},
'underscore': {
exports: '_'
},
'foo': {
deps: ['bar'],
exports: 'Foo',
init: function (bar) {
//Using a function allows you to call noConflict for
//libraries that support it, and do other cleanup.
//However, plugins for those libraries may still want
//a global. "this" for the function will be the global
//object. The dependencies will be passed in as
//function arguments. If this function returns a value,
//then that value is used as the module export value
//instead of the object found via the 'exports' string.
//Note: jQuery registers as an AMD module via define(),
//so this will not work for jQuery. See notes section
//below for an approach for jQuery.
return this.Foo.noConflict();
}
}
}
});
//Then, later in a separate file, call it 'MyModel.js', a module is
//defined, specifying 'backbone' as a dependency. RequireJS will use
//the shim config to properly load 'backbone' and give a local
//reference to this module. The global Backbone will still exist on
//the page too.
define(['backbone'], function (Backbone) {
return Backbone.Model.extend({});
});
@caot
Copy link
Author

caot commented Oct 8, 2019

RequireJS is a JavaScript file and module loader. https://requirejs.org/docs/api.html#config-shim
dygraphs is a fast, flexible open source JavaScript charting library. http://dygraphs.com/
Plottable is a library of chart components for creating flexible, custom charts for websites. https://github.com/palantir/plottable
D3.js is a JavaScript library for manipulating documents based on data https://d3js.org/

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