Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 12:11
Using RequireJS For Asynchronous Script Loading And JavaScript Dependency Management
<!DOCTYPE html>
<html>
<head>
<title>
Playing With RequireJS For JavaScript Dependency Management
</title>
<!--
Load the RequireJS library; the "data-main" attribute
will tell the RequireJS library which JavaScript file to
load (main.js) after itself has loaded.
-->
<script
type="text/javascript"
src="./require.js"
data-main="./main">
</script>
</head>
<body>
<!-- Left intentionally blank. -->
</body>
</html>
// Log the start of the file.
console.log( "START: main.js" );
// This is our main script. It will run once RequireJS (and this
// file) have been loaded into the page. It has some class
// dependencies for execution.
//
// NOTE: jQuery isn't loaded as a module; it's simply added to the
// global name-space.
require(
[
"jquery-1.6.4.js",
"friend",
"pet",
"cat-lover"
],
function( _jquery_, Friend, Pet, CatLover ) {
// Create a cat-lover.
var sarah = new CatLover( "Sarah" );
// Create a cat.
var mrMittens = new Pet( Pet.CAT, "Mr. Mittens" );
// Associate the kitty with the cat-lover.
sarah.setPet( mrMittens );
// Log a success.
console.log( "Oh " + sarah.getPet().getName() + "! You're so cute!" );
// Log that jquery was loaded into the global name-space.
console.log( "jQuery", $.fn.jquery, "loaded!" );
}
);
// Log the end of the file.
console.log( "END: main.js" );
// Define the Friend class. This depends on the existence of the
// jQuery library for its definition.
//
// NOTE: jQuery isn't loaded as a module; it's simply added to the
// global name-space.
define(
[
"jquery-1.6.4.js"
],
function( _jquery_ ){
// I am an internal counter for ID.
var instanceID = 0;
// I am the class constructor.
function Friend( name ){
// Get the instance ID.
this._id = ++instanceID;
// Store the name value.
this._name = name;
// Store a default value for pet.
this._pet = null;
}
// Define the prototype.
Friend.prototype = {
// I return the id.
getID: function(){
return( this._id );
},
// I return the name.
getName: function(){
return( this._name );
},
// I return the current pet.
getPet: function(){
return( this._pet );
},
// I set the new name.
setName: function( name ){
// Store the new value.
this._name = name;
// Return this object reference for method chaining.
return( this );
},
// I set the new pet.
setPet: function( pet ){
// Store the new value.
this._pet = pet;
// Return this object reference for method chaining.
return( this );
}
};
// -------------------------------------------------- //
// -------------------------------------------------- //
// Return the constructor.
return( Friend );
}
);
// Define the Pet class. This depends on the existence of the
// jQuery library for its definition.
//
// NOTE: jQuery isn't loaded as a module; it's simply added to the
// global name-space.
define(
[
"jquery-1.6.4.js"
],
function( _jquery_ ){
// I am an internal counter for ID.
var instanceID = 0;
// I am the class constructor.
function Pet( type, name ){
// Get the instance ID.
this._id = ++instanceID;
// Store the type.
this._type = type;
// Store the name value.
this._name = name;
}
// Define some constants.
Pet.CAT = 1;
Pet.DOG = 2;
Pet.BIRD = 3;
Pet.FISH = 4;
Pet.RODENT = 5;
// Define the prototype.
Pet.prototype = {
// I return the id.
getID: function(){
return( this._id );
},
// I return the name.
getName: function(){
return( this._name );
},
// I return the current type.
getType: function(){
return( this._type );
},
// I set the new name.
setName: function( name ){
// Store the new value.
this._name = name;
// Return this object reference for method chaining.
return( this );
}
};
// -------------------------------------------------- //
// -------------------------------------------------- //
// Return the constructor.
return( Pet );
}
);
// Define the Cat Lover class. Since this is a specialized version of
// a Friend (that loves a certain kind of animal), it depends on the
// existence of both the Friend and Pet classes.
//
// NOTE: jQuery isn't loaded as a module; it's simply added to the
// global name-space.
define(
[
"jquery-1.6.4.js",
"friend",
"pet"
],
function( _jquery_, Friend, Pet ){
// I am the class constructor.
function CatLover( name ){
// Call the super constructor.
Friend.call( this, name );
}
// Extend the Friend class.
CatLover.prototype = Object.create( Friend.prototype );
// Override the the setPet() to make sure that the type
// is a Cat - d'uh; I mean come on... cat's are amazing.
// If you're not a cat lover, in some sense, you're just
// fooling yourself.
CatLover.prototype.setPet = function( pet ){
// Make sure the pet is the good kind.
if (pet.getType() !== Pet.CAT){
// WTF?!
throw( new Error( "InsufficientPetAwesomeness" ) );
}
// This Pet is the right kind. Pass off to super class.
return(
Friend.prototype.setPet.call( this, pet )
);
};
// -------------------------------------------------- //
// -------------------------------------------------- //
// Return the constructor.
return( CatLover );
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment