Skip to content

Instantly share code, notes, and snippets.

@quipu
Created June 16, 2014 08:58
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 quipu/104a697407d6507904ae to your computer and use it in GitHub Desktop.
Save quipu/104a697407d6507904ae to your computer and use it in GitHub Desktop.
Example module using Asynchronous Module Definition (AMD) design pattern.
define(function() {
var _counter = 0;
function generateId() {
return "customId" + _counter++;
}
function create(tagName, id) {
var el = document.createElement(tagName);
el.id = id || generateId();
return el;
}
return {
generateId : generateId,
create : create
}
});
@quipu
Copy link
Author

quipu commented Jun 16, 2014

To use this module you'll require using a scriptloader like RequireJS.

require(['myAMDModule'], function(myMod) {
    var el1 = myMod.create('div', 'myElement');
    var el2 = myMod.create('div');

    console.log(el1.id);
    console.log(el2.id);
});

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