Download presentation
Presentation is loading. Please wait.
Published byRoderick Simmons Modified over 9 years ago
1
“RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.” IE 6+.......... compatible ✔ Firefox 2+..... compatible ✔ Safari 3.2+.... compatible ✔ Chrome 3+...... compatible ✔ Opera 10+...... compatible ✔ All good!!!!
2
Why do we use Modules? For better decoupling and maintainability Reusability To avoid naming collisions To facilitate unit testing
3
Module Pattern var MODULE = (function () { var my = {}, privateVariable = 1; my.moduleProperty = 1; my.getPrivateVariable = function () { return privateVariable; }; return my; }()); MODULE.getPrivateVariable(); http://addyosmani.com/writing-modular-js/
4
What are AMD modules and why should we think about using them? Asynchronous Module Definition keeps the global namespace free less cumbersome to keep track of file dependencies encourages decoupling dynamic code loading (ie. If(true) loadThisFile() ) http://requirejs.org/docs/whyamd.html
5
AMD Module definition define([“dependency”], function(dependency) { var value; function init() { value = dependency.getValue(); } return { init:init }; });
6
Common JS Module definition define( function(require, exports, module) { var value, dependency = require(‘dependency1’); function init() { value = dependency.getValue(); } exports.init = init; });
7
simplified CommonJS wrapping define( function(require) { var value, dependency = require(‘dependency’); function init() { value = dependency.getValue(); } return { init:init }; }); https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified- CommonJS-wrapper-and-standard-AMD-define
8
Returning name/value objects define( { color: “orange”, size: “medium” });
9
Returning Constructor Functions define(function(require) { function Model( id ) { var self = this, name = “Model: ” + id; this.getName = function() { return name; } return Model; });
10
main.js – require.config({}) require.config({ paths: { // map shared business logic MainGameController : "components/com_pchblackjack_content/assets/js/…, // map template specific views MainGameView : "templates/pchblackjack_desktop/js/game/MainGameView", } }); require( ["MainGameController"], function (controller) { controller.init(); });
11
text!plugin Loads in html as a dependency – define(["text!howtoplaytmpl"], function(tmpl) In-lined into optimized build – define('text!howtoplaytmpl',[],function () { return ' \r\n\t } http://requirejs.org/docs/plugins.html
12
Dependency Injection swappable implementations – Use a mock service to simulate data using the map config. Let’s try using SoundJS instead of HowlerJS!
13
Optimization Use Node.js or Java to build your project Useful links: – Explains the basics: http://requirejs.org/docs/optimization.html – Show all options: https://github.com/jrburke/r.js/blob/master/build /example.build.js Time for some Grunt work!
14
Using Grunt with Node.js Install node.js npm install –g grunt cli package.json – Gather your tools – npm install [module] –save-dev Gruntfile.js – Create your requirejs task
15
Too many options! The optimization process can be daunting. Start early! Why can’t I load dependencies dynamically after optimization? AppDir, baseUrl, dir, … durrrrrrrr Shim – Stay away if possible Wrap.startFile/endFile – keep all your code in anonymous function https://github.com/jrburke/r.js/blob/master/build/example.build.js
16
Continued Reading Check out Backbone.js and how requirejs can be used effectively. – http://backbonetutorials.com/organizing- backbone-using-modules/ – http://addyosmani.github.io/backbone- fundamentals/#wrapping-models-views-and- other-components-with-amd
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.