31
Managing JavaScript Dependencies with RequireJS Den Odell

Managing JavaScript Dependencies With RequireJS

Embed Size (px)

DESCRIPTION

Learn how to manage and dynamically load JavaScript code files and their dependencies in a robust, scalable way within your large web sites and applications using the RequireJS library.

Citation preview

Page 1: Managing JavaScript Dependencies With RequireJS

Managing JavaScript Dependencies with

RequireJS

Den Odell

Page 2: Managing JavaScript Dependencies With RequireJS

LibrariesjQuery, Modernizr, ...

FrameworksBackbone, Ember, ...

Reusable Plugin and Utility ScriptsjQuery plugins, TypeKit, Underscore, ...

Custom Application Code

Page 3: Managing JavaScript Dependencies With RequireJS

<script src="/assets/js/lib/jquery-1.7.1.min.js"></script><script src="/assets/js/lib/jquery-ui-1.8.17.custom.min.js"></script><script src="/assets/js/global.js"></script><script src="/assets/js/breaking-news.js"></script><script src="/assets/js/lib/jquery.colorbox.js"></script><script src="/assets/js/modal.js"></script><script src="http://use.typekit.com/did3rrm.js"></script><!--[if lt IE 9]> <script src="/assets/js/lib/cssSandpaper/EventHelpers.js"></script> <script src="/assets/js/lib/cssSandpaper/cssQuery-p.js"></script> <script src="/assets/js/lib/cssSandpaper/jcoglan.com/sylvester.js"></script> <script src="/assets/js/lib/cssSandpaper/cssSandpaper.js"></script> <script src="/assets/js/lib/jquery-extended-selectors.js"></script> <script src="/assets/js/lib/selectivizr-min.js"></script><![endif]--> <script src="/assets/js/lib/bgpos.js"></script><script src="http://w.sharethis.com/button/buttons.js"></script><script src="/assets/js/lib/yepnope.css-prefix.js"></script><script src="/assets/js/feature-carousel.js"></script><script src="/assets/js/dropdown.js"></script><script src="/assets/js/lib/jquery.ui.selectmenu.js"></script><script src="/assets/js/lib/jquery.selectmenu.js"></script><script src="/assets/js/lib/swfobject.js"></script><script src="/assets/js/flashembed.js"></script><script src="/assets/js/lib/jquery.jqplugin.1.0.2.min.js"></script><script src="/assets/js/audioplayer.js"></script><script src="/assets/js/game-tray.js"></script><script src="/assets/js/tracking.js"></script><script src="/assets/js/lib/time-tracker.js"></script>

Page 4: Managing JavaScript Dependencies With RequireJS

More JavaScript typically means more complexity

Page 5: Managing JavaScript Dependencies With RequireJS

RequireJS Modules & Dependencies

Page 6: Managing JavaScript Dependencies With RequireJS
Page 7: Managing JavaScript Dependencies With RequireJS

Let’s build a signup form!

Page 8: Managing JavaScript Dependencies With RequireJS
Page 9: Managing JavaScript Dependencies With RequireJS

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Mailing list</title>

<link rel="stylesheet" href="styles/main.css">

</head>

<body>

<form action="thank-you.html" id="form" method="post">

<h1>Join our mailing list</h1>

<label for="email">Enter your email address</label>

<input type="text" name="email" id="email" placeholder="e.g. [email protected]">

<input type="submit" value="Sign up">

</form>

</body>

</html>

Page 10: Managing JavaScript Dependencies With RequireJS

http://requirejs.org

Current version: 2.1.4Support: IE6+, FF2+, Safari 3.2+, Chrome

3+, Opera 10+Size: 5.5KB min+gzip

Page 11: Managing JavaScript Dependencies With RequireJS

1. Listen for ‘submit’ event on the form

2. Validate the format of the email address provided

3. If the format is valid, allow the form to submit to the server

4. If the format is not valid, highlight the error and prevent the form submitting

Page 12: Managing JavaScript Dependencies With RequireJS

1. Listen for ‘submit’ event on the form

2. Validate the format of the email address provided

3. If the format is valid, allow the form to submit to the server

4. If the format is not valid, highlight the error and prevent the form submitting

Main application script

jQuery

Validation plugin for jQuery

Page 13: Managing JavaScript Dependencies With RequireJS

jQueryValidation plugin for jQuery

Main application script

Page 14: Managing JavaScript Dependencies With RequireJS

jQueryValidation plugin for jQuery

Main application script

Dependencies

Page 15: Managing JavaScript Dependencies With RequireJS
Page 16: Managing JavaScript Dependencies With RequireJS

<script src="scripts/require.js" data-main="scripts/main"></script>

Adding RequireJS to HTML

Page 17: Managing JavaScript Dependencies With RequireJS

define(

moduleName, // optional, defaults to name of file

dependencies, // optional array listing dependencies

function(params) {

// Function to execute once dependencies have been loaded

// params contains return values from the dependencies

}

);

Defining a code module in RequireJS

Page 18: Managing JavaScript Dependencies With RequireJS

Example of a code module in RequireJS

define(["lib/jquery-1.9.0"], function($) {

// Do something with jQuery as $

});

Page 19: Managing JavaScript Dependencies With RequireJS

Creating a module mapping for jQuery in main.js

requirejs.config({

paths: {

"jquery": "lib/jquery-1.9.0”

}

});

Page 20: Managing JavaScript Dependencies With RequireJS
Page 21: Managing JavaScript Dependencies With RequireJS

Extending the module mapping for jQuery in main.js

requirejs.config({

paths: {

"jquery": [

"https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min",

// If the CDN fails, load from this local module instead

"lib/jquery-1.9.0"

]

}

});

Page 22: Managing JavaScript Dependencies With RequireJS

jQuery Validation Plug-in Modulescripts/lib/validation-plugin.js

Page 23: Managing JavaScript Dependencies With RequireJS

define(["jquery"], function($) {

$.fn.isValidEmail = function() {

var isValid = true,

regEx = /\S+@\S+\.\S+/;

this.each(function() {

if (!regEx.test(this.value)) {

isValid = false;

}

});

return isValid;

};

});

scripts/lib/validation-plugin.js

Page 24: Managing JavaScript Dependencies With RequireJS

Main application scriptscripts/lib/main.js

Page 25: Managing JavaScript Dependencies With RequireJS

scripts/lib/main.jsrequire(["jquery", "lib/validation-plugin"], function($) {

var $form = $("#form”),

$email = $("#email");

$form.on("submit", function(e) {

e.preventDefault();

if ($email.isValidEmail()) {

$form.get(0).submit();

} else {

$email.addClass("error").focus();

}

});

$email.on("keyup", function() {

$email.removeClass("error");

});

});

Page 26: Managing JavaScript Dependencies With RequireJS

Improving page load performance...

Page 27: Managing JavaScript Dependencies With RequireJS

scripts/lib/main.jsrequire(["jquery"], function($) {

var $form = $("#form"),

$email = $("#email");

$form.on("submit", function(e) {

e.preventDefault();

require(["lib/validation-plugin"], function() {

if ($email.isValidEmail()) {

$form.get(0).submit();

} else {

$email.addClass("error").focus();

}

});

});

$email.on("keyup", function() {

$email.removeClass("error");

});

});

Page 28: Managing JavaScript Dependencies With RequireJS
Page 29: Managing JavaScript Dependencies With RequireJS

What else can RequireJS do?

Page 30: Managing JavaScript Dependencies With RequireJS

Some Useful Plug-ins

i18ntext

handlebarsfont

cache

Page 31: Managing JavaScript Dependencies With RequireJS

Thank You