Code Monkey home page Code Monkey logo

use.js's Introduction

use.js

Created by Tim Branyen @tbranyen

Using AMD shouldn't be painful, but unfortunately like many facets of life, using incompatible code doesn't work out the way you would expect. Especially when they have dependencies of their own.

Use.js has been written to work with RequireJS specifically, but its possible that it will work with other AMD loaders/builders as well.

I like RequireJS and I like AMD, this plugin addresses the issue that many others have tackled in the wrong way. Before this plugin it was common to directly modify library source code, create separate "shim" files, or load the scripts without regard for their dependencies or associated object.

This plugin does not modify library source code and executes the scripts as they were intended and tested. It simply ensures the proper dependencies have loaded and attaches the specified global object to the module definition.

Download & Include

Development is fully commented source, Production is minified and stripped of all comments except for license/credits.

You need to set the path of use to its location (I typically put it in a plugins folder)

require.config({
  paths: {
    "use": "path/to/use"
  }
});

Usage

You must define a configuration for each incompatible script you wish to include.

Configuration format

The string property used in attach will resolve to window[stringProp] Functions are evaluated in the scope of the window and passed all dependencies as arguments.

require.config({
 use: {
   "underscore": {
     attach: "_"
   },

   "backbone": {
     deps: ["use!underscore", "jquery"],
     attach: function(_, $) {
       return Backbone;
     }
   }
 }
});

Requiring a module

To require a module you simply use the require function as usual, except prefix the script name with use! the ! tells the loader its a plugin.

require(["use!backbone"], function(Backbone) {
  console.log(Backbone); // is Backbone!
});

Release History

0.3.0

  • Dojo AMD loader support
  • Node.js (maybe others w/o window object) compatibility
  • Several fixes with how r.js builds out modules
  • Support for wrap=true

0.2.0

  • Added write method that ensures compatibility in loader environments that do not give access to the configuration object
  • Published to GitHub as a repo

0.1.0

  • Initial release, allows non-amd compatible JavaScript to load
  • Gist only

use.js's People

Contributors

tbranyen avatar kmiyashiro avatar christopherscott avatar iammerrick avatar russelldavis avatar

Watchers

Alex Il avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.