Skip to content

Instantly share code, notes, and snippets.

@jessecravens
Created March 29, 2012 15:01
Show Gist options
  • Save jessecravens/2238250 to your computer and use it in GitHub Desktop.
Save jessecravens/2238250 to your computer and use it in GitHub Desktop.
JavaScript Modules - yui3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Modules</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>
<script>
YUI.add('my-module', function (Y) {
//..
});
YUI().use('my-module', function (Y) {
//..
});
</script>
</head>
<body>
</body>
</html>
MyRemoteModule = {
logLoaded: function () {
console.log('yui3 my-remote-module loaded!');
}
}
YUI({
modules: {
'my-remote-module': {
fullpath: "javascripts/my-remote-module.js"
}
}
// 3.x node will be dynamically loaded so we can work with DOM elements
}).use('my-remote-module', function(Y, result) {
// The callback supplied to use() will be executed regardless of
// whether the operation was successful or not. The second parameter
// is a result object that has the status of the operation. We can
// use this to try to recover from failures or timeouts.
if (!result.success) {
Y.log('Load failure: ' + result.msg, 'warn', 'Example');
} else {
MyRemoteModule.logLoaded();
}
});
YUI.add('my-module', function (Y) {
// Write your module code here, and make your module available on the Y
// object if desired.
Y.MyModule = {
logLoaded: function () {
console.log('yui3 my-module loaded!');
}
},
'0.0.1', {
requires: ['node', 'event']
}
});
YUI().use('my-module', function (Y) {
// The Y instance here is the same Y instance that was passed into
// my-module's add() callback, so the Y.MyModule object that was created
// there is now available here as well.
Y.MyModule.logLoaded();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment