Created
March 7, 2012 05:11
-
-
Save jessecravens/1991043 to your computer and use it in GitHub Desktop.
JavaScript Modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Standard Java | |
import javax.xml.parsers.*; | |
import mypackage.MyClass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(["jquery", "jquery.alpha", "jquery.beta"], function($) { | |
//the jquery.alpha.js and jquery.beta.js plugins have been loaded. | |
$(function() { | |
$('body').alpha().beta(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.alpha = function() { | |
return this.append('<p>Alpha is Go!</p>'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(["order!one", "order!two", "order!three"], function() { | |
console.log('load complete'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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