Skip to content

Instantly share code, notes, and snippets.

@jessecravens
Created March 7, 2012 05:11
Show Gist options
  • Save jessecravens/1991043 to your computer and use it in GitHub Desktop.
Save jessecravens/1991043 to your computer and use it in GitHub Desktop.
JavaScript Modules
// Causes problems
// We need synchronous, but bad idea for performance
var Employee = require("types/Employee");
function Manager () {
this.reports = [];
}
//Error if require call is async
Manager.prototype = new Employee();
// Defining a Module
// making the index method accessible
exports.index = function(req, res){
res.render('index', { title: 'JavaScript Modules Demo' })
};
exports.desktop = function(req, res){
res.render('desktop', { layout: 'basic' });
};
exports.iphone = function(req, res){
res.render('iphone', { layout: 'mobile' });
};
// Requiring a Module
var express = require('express')
, routes = require('./routes')
, io = require('socket.io')
, useragent = require('useragent')
, gm = require('googlemaps')
// Use the Module
app.get('/', function(req, res){
res.render("index");
});
// OR, After Device Detection
app.get('/', function(req, res) {
routes.desktop(req, res);
var ua = useragent.is(req.headers['user-agent'])
switch(true)
{
case ua.chrome:
console.log('within ROOT route / detected as chrome desktop');
routes.desktop(req, res);
break;
case ua.mobile_safari:
console.log('within ROOT route / detected as mobile_safari');
routes.iphone(req, res);
break;
default:
console.log('within ROOT route / fallback to default');
routes.desktop(req, res);
}
});
define(
//The name of this module
"types/Manager",
//The array of dependencies
["types/Employee"],
//The function to execute when all dependencies have loaded. The arguments
//to this function are the array of dependencies mentioned above.
function (Employee) {
function Manager () {
this.reports = [];
}
//This will now work
Manager.prototype = new Employee();
//return the Manager constructor function so it can be used by other modules.
return Manager;
}
);
// Standard Java
import javax.xml.parsers.*;
import mypackage.MyClass;
require(["jquery", "jquery.alpha", "jquery.beta"], function($) {
//the jquery.alpha.js and jquery.beta.js plugins have been loaded.
$(function() {
$('body').alpha().beta();
});
});
<!DOCTYPE html>
<html>
<head>
<title>jQuery+RequireJS Sample Page</title>
<!-- This is a special version of jQuery with RequireJS built-in -->
<script data-main="scripts/main" src="scripts/require-jquery.js"></script>
</head>
<body>
<h1>jQuery+RequireJS Sample Page</h1>
<p>Look at source or inspect the DOM to see how it works.</p>
</body>
</html>
$.fn.alpha = function() {
return this.append('<p>Alpha is Go!</p>');
};
require(["order!one", "order!two", "order!three"], function() {
console.log('load complete');
});
// differs from the normal CommonJS syntax out of necessity to work well in the browser.
require(["some/script.js"], function() {
//This function is called after some/script.js has loaded.
});
YUI.add('my-module', function (Y) {
// ...
}, '0.0.1', {
requires: ['node', 'event']
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment