SlideShare a Scribd company logo
1 of 17
Write a Better Java Script 
with RequireJS 
For Reference 
http://requirejs.org
Why use a tool like RequireJS? 
Large applications often require a number of JavaScript files. 
Generally, they are loaded one by one using <script> tags. 
Additionally, each file can potentially be dependent on other files.
JS sucks 
. 
├── ckeditor 
│ ├── ckeditor.js 
│ ├── contents.css 
│ ├── lang 
│ │ ├── af.js 
│ │ ├── ar.js 
│ │ ├── bg.js 
│ │ ├── bn.js 
│ │ ├── bs.js 
│ │ ├── ca.js 
│ │ ├── cs.js 
│ │ ├── cy.js 
│ │ ├── da.js 
│ │ ├── de.js 
│ │ ├── el.js 
│ │ ├── en-au.js 
│ │ ├── en-ca.js 
│ │ ├── en-gb.js 
│ │ ├── en.js 
│ │ ├── eo.js 
│ │ ├── es.js 
│ │ ├── et.js 
│ │ ├── eu.js 
│ │ ├── fa.js 
│ │ ├── fi.js 
│ │ ├── fo.js 
│ │ ├── fr-ca.js 
│ │ ├── hi.js 
│ │ ├── hr.js 
│ │ ├── hu.js 
│ │ ├── is.js 
│ │ ├── it.js 
│ │ ├── ja.js 
│ │ ├── ka.js 
│ │ ├── km.js 
│ │ ├── ko.js 
│ │ ├── _languages.js 
│ │ ├── lt.js 
│ │ ├── lv.js 
│ │ ├── mn.js 
│ │ ├── ms.js 
│ │ ├── nb.js 
│ │ ├── nl.js 
│ │ ├── no.js 
│ │ ├── pl.js 
│ │ ├── pt-br.js 
│ │ ├── pt.js 
│ │ ├── ro.js 
│ │ ├── ru.js 
│ │ ├── sk.js 
│ │ ├── sl.js 
│ │ ├── sr.js 
│ │ ├── sr-latn.js 
│ │ ├── sv.js 
│ │ ├── th.js 
│ │ ├── _trans.txt 
│ │ ├── tr.js 
│ │ ├── uk.js 
│ │ ├── vi.js 
│ │ └── default.js 
│ ├── skins 
│ │ └── kama 
│ │ ├── dialog.css 
│ │ ├── editor.css 
│ │ ├── icons.png 
│ │ ├── icons_rtl.png 
│ │ ├── images 
│ │ │ ├── dialog_sides.gif 
│ │ │ ├── dialog_sides.png 
│ │ │ ├── dialog_sides_rtl.png 
│ │ │ ├── mini.gif 
│ │ │ ├── noimage.png 
│ │ │ ├── sprites_ie6.png 
│ │ │ ├── sprites.png 
│ │ │ └── toolbar_start.gif 
│ │ ├── skin.js 
│ │ └── templates.css 
│ └── SQRLY_TEAM_PLEASE_README__LICENSE 
├── elevationservice_eg_google.js 
├── jquery.form.js 
├── jquery.history.js 
├── jqueryplugins 
│ ├── jquery.address-1.3.js 
│ └── jquery.simplemodal-1.3.min.js 
├── markers.js 
├── pages 
│ ├── add-park.js 
│ ├── datacleaning.js 
│ └── park-detail.js 
├── park-ratings.js 
├── polylinearray_eg_google.js
JS sucks 
1. code is too long 
2. just a little code from somewhere else 
3. copy and paste 
4. goto: 1
RequireJS makes Js 
• Help you make your code more modular 
• manages script loading for you 
• build and compress your code 
(oh, and other stuff too...)
What is RequireJS?
What is RequireJS? 
• Modular programming is used to break large applications into 
smaller blocks of manageable code 
• Module based coding eases the effort for maintenance and 
increases reusability. 
• managing dependencies between modules is a major concern 
developers face throughout the application development 
process. 
• RequireJS is one of the most popular frameworks around for 
managing dependencies between modules
What is RequireJS? 
• Async Script Loader 
• 12 k 
• Very actively developed 
• Well documented
Making code more modular
So what's the code look like?
Loading from the page 
<script data-main="js/main" 
src="scripts/require.js"></script> 
main.js is the entry point of the app
Loading from the page 
require(["pages/profile"], function(Profile) { 
...do stuff... 
});
Defining a Module 
define(["foo", "baz", "x"], function(Foo, Baz) { 
...do stuff... 
return { 
init: function(data) { 
container = data; 
}, 
do_something: some_internal_method 
} 
});
Let's look at an example
Thanks

More Related Content

What's hot

Zend Framework Workshop Parte1
Zend Framework Workshop Parte1Zend Framework Workshop Parte1
Zend Framework Workshop Parte1
massimiliano.wosz
 
Slide typescript - net campus
Slide typescript - net campusSlide typescript - net campus
Slide typescript - net campus
DotNetCampus
 
Zend Framework Workshop Parte2
Zend Framework Workshop Parte2Zend Framework Workshop Parte2
Zend Framework Workshop Parte2
massimiliano.wosz
 
Corso WebApp iOS - Lezione 06: Web Development for iOS Devices
Corso WebApp iOS - Lezione 06:   Web Development for iOS DevicesCorso WebApp iOS - Lezione 06:   Web Development for iOS Devices
Corso WebApp iOS - Lezione 06: Web Development for iOS Devices
Andrea Picchi
 

What's hot (20)

AngularJS-Intro
AngularJS-IntroAngularJS-Intro
AngularJS-Intro
 
Sviluppo web con Ruby on Rails
Sviluppo web con Ruby on RailsSviluppo web con Ruby on Rails
Sviluppo web con Ruby on Rails
 
Workshop angular
Workshop angularWorkshop angular
Workshop angular
 
Creare API pubbliche, come evitare gli errori comuni
 Creare API pubbliche, come evitare gli errori comuni Creare API pubbliche, come evitare gli errori comuni
Creare API pubbliche, come evitare gli errori comuni
 
jQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesignerjQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesigner
 
Zend Framework Workshop Parte1
Zend Framework Workshop Parte1Zend Framework Workshop Parte1
Zend Framework Workshop Parte1
 
Sviluppo applicazioni in Ruby on Rails
Sviluppo applicazioni in Ruby on RailsSviluppo applicazioni in Ruby on Rails
Sviluppo applicazioni in Ruby on Rails
 
Slide typescript - net campus
Slide typescript - net campusSlide typescript - net campus
Slide typescript - net campus
 
CommitUniversity AngularJSAdvanced Andrea Vallotti
CommitUniversity  AngularJSAdvanced Andrea VallottiCommitUniversity  AngularJSAdvanced Andrea Vallotti
CommitUniversity AngularJSAdvanced Andrea Vallotti
 
Powerful asp.net 4 e ie9
Powerful asp.net 4 e ie9Powerful asp.net 4 e ie9
Powerful asp.net 4 e ie9
 
ModulAngular
ModulAngularModulAngular
ModulAngular
 
Web frameworks
Web frameworksWeb frameworks
Web frameworks
 
Zend Framework Workshop Parte2
Zend Framework Workshop Parte2Zend Framework Workshop Parte2
Zend Framework Workshop Parte2
 
Il Web orientato al futuro: Express, Angular e nodeJS
Il Web orientato al futuro: Express, Angular e nodeJS Il Web orientato al futuro: Express, Angular e nodeJS
Il Web orientato al futuro: Express, Angular e nodeJS
 
jQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesignerjQuery - 1 | WebMaster & WebDesigner
jQuery - 1 | WebMaster & WebDesigner
 
ASP.NET Core - dove siamo arrivati
ASP.NET Core - dove siamo arrivatiASP.NET Core - dove siamo arrivati
ASP.NET Core - dove siamo arrivati
 
Novità di Asp.Net 4.0
Novità di Asp.Net 4.0Novità di Asp.Net 4.0
Novità di Asp.Net 4.0
 
Workshop Ideare e creare Web Applications, Introduzione ad AngularJS
Workshop Ideare e creare Web Applications, Introduzione ad AngularJSWorkshop Ideare e creare Web Applications, Introduzione ad AngularJS
Workshop Ideare e creare Web Applications, Introduzione ad AngularJS
 
Corso WebApp iOS - Lezione 06: Web Development for iOS Devices
Corso WebApp iOS - Lezione 06:   Web Development for iOS DevicesCorso WebApp iOS - Lezione 06:   Web Development for iOS Devices
Corso WebApp iOS - Lezione 06: Web Development for iOS Devices
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 

Similar to Require js

Come sviluppare applicazioni cross device con HTML
Come sviluppare applicazioni cross device con HTMLCome sviluppare applicazioni cross device con HTML
Come sviluppare applicazioni cross device con HTML
Sinergia Totale
 
Dominare il codice legacy
Dominare il codice legacyDominare il codice legacy
Dominare il codice legacy
Tommaso Torti
 
Netbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHPNetbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHP
Giorgio Cefaro
 
Introduzione a TypeScript
Introduzione a TypeScriptIntroduzione a TypeScript
Introduzione a TypeScript
Sinergia Totale
 
DotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScriptDotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScript
Sinergia Totale
 

Similar to Require js (20)

Node.js - Server Side Javascript
Node.js - Server Side JavascriptNode.js - Server Side Javascript
Node.js - Server Side Javascript
 
Javascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesignerJavascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesigner
 
Introduzione a JavaScript e jQuery (1/2)
Introduzione a JavaScript e jQuery (1/2)Introduzione a JavaScript e jQuery (1/2)
Introduzione a JavaScript e jQuery (1/2)
 
node.js e Postgresql
node.js e Postgresqlnode.js e Postgresql
node.js e Postgresql
 
Come sviluppare applicazioni cross device con HTML
Come sviluppare applicazioni cross device con HTMLCome sviluppare applicazioni cross device con HTML
Come sviluppare applicazioni cross device con HTML
 
NuIEEE - Workshop de Git
NuIEEE - Workshop de GitNuIEEE - Workshop de Git
NuIEEE - Workshop de Git
 
Dominare il codice legacy
Dominare il codice legacyDominare il codice legacy
Dominare il codice legacy
 
ASP.NET, ottimizziamo con la cache
ASP.NET, ottimizziamo con la cacheASP.NET, ottimizziamo con la cache
ASP.NET, ottimizziamo con la cache
 
Javascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesignerJavascript - 1 | WebMaster & WebDesigner
Javascript - 1 | WebMaster & WebDesigner
 
Creazione componenti con Vue js
Creazione componenti con Vue jsCreazione componenti con Vue js
Creazione componenti con Vue js
 
Netbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHPNetbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHP
 
Techbar nodejs+mongodb+mongoose
Techbar nodejs+mongodb+mongooseTechbar nodejs+mongodb+mongoose
Techbar nodejs+mongodb+mongoose
 
Building Large Java Codebase with Bazel - CodeOne
Building Large Java Codebase with Bazel - CodeOneBuilding Large Java Codebase with Bazel - CodeOne
Building Large Java Codebase with Bazel - CodeOne
 
Introduzione a TypeScript
Introduzione a TypeScriptIntroduzione a TypeScript
Introduzione a TypeScript
 
Portofino per Jug Padova
Portofino per Jug PadovaPortofino per Jug Padova
Portofino per Jug Padova
 
Bootstrapはこう使う
Bootstrapはこう使うBootstrapはこう使う
Bootstrapはこう使う
 
DotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScriptDotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScript
 
DDive - 8.5.2 Xpages - L'evoluzione continua
DDive - 8.5.2 Xpages - L'evoluzione continuaDDive - 8.5.2 Xpages - L'evoluzione continua
DDive - 8.5.2 Xpages - L'evoluzione continua
 
#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2
 
Java introduzione
Java introduzioneJava introduzione
Java introduzione
 

Require js

  • 1. Write a Better Java Script with RequireJS For Reference http://requirejs.org
  • 2.
  • 3. Why use a tool like RequireJS? Large applications often require a number of JavaScript files. Generally, they are loaded one by one using <script> tags. Additionally, each file can potentially be dependent on other files.
  • 4. JS sucks . ├── ckeditor │ ├── ckeditor.js │ ├── contents.css │ ├── lang │ │ ├── af.js │ │ ├── ar.js │ │ ├── bg.js │ │ ├── bn.js │ │ ├── bs.js │ │ ├── ca.js │ │ ├── cs.js │ │ ├── cy.js │ │ ├── da.js │ │ ├── de.js │ │ ├── el.js │ │ ├── en-au.js │ │ ├── en-ca.js │ │ ├── en-gb.js │ │ ├── en.js │ │ ├── eo.js │ │ ├── es.js │ │ ├── et.js │ │ ├── eu.js │ │ ├── fa.js │ │ ├── fi.js │ │ ├── fo.js │ │ ├── fr-ca.js │ │ ├── hi.js │ │ ├── hr.js │ │ ├── hu.js │ │ ├── is.js │ │ ├── it.js │ │ ├── ja.js │ │ ├── ka.js │ │ ├── km.js │ │ ├── ko.js │ │ ├── _languages.js │ │ ├── lt.js │ │ ├── lv.js │ │ ├── mn.js │ │ ├── ms.js │ │ ├── nb.js │ │ ├── nl.js │ │ ├── no.js │ │ ├── pl.js │ │ ├── pt-br.js │ │ ├── pt.js │ │ ├── ro.js │ │ ├── ru.js │ │ ├── sk.js │ │ ├── sl.js │ │ ├── sr.js │ │ ├── sr-latn.js │ │ ├── sv.js │ │ ├── th.js │ │ ├── _trans.txt │ │ ├── tr.js │ │ ├── uk.js │ │ ├── vi.js │ │ └── default.js │ ├── skins │ │ └── kama │ │ ├── dialog.css │ │ ├── editor.css │ │ ├── icons.png │ │ ├── icons_rtl.png │ │ ├── images │ │ │ ├── dialog_sides.gif │ │ │ ├── dialog_sides.png │ │ │ ├── dialog_sides_rtl.png │ │ │ ├── mini.gif │ │ │ ├── noimage.png │ │ │ ├── sprites_ie6.png │ │ │ ├── sprites.png │ │ │ └── toolbar_start.gif │ │ ├── skin.js │ │ └── templates.css │ └── SQRLY_TEAM_PLEASE_README__LICENSE ├── elevationservice_eg_google.js ├── jquery.form.js ├── jquery.history.js ├── jqueryplugins │ ├── jquery.address-1.3.js │ └── jquery.simplemodal-1.3.min.js ├── markers.js ├── pages │ ├── add-park.js │ ├── datacleaning.js │ └── park-detail.js ├── park-ratings.js ├── polylinearray_eg_google.js
  • 5. JS sucks 1. code is too long 2. just a little code from somewhere else 3. copy and paste 4. goto: 1
  • 6.
  • 7. RequireJS makes Js • Help you make your code more modular • manages script loading for you • build and compress your code (oh, and other stuff too...)
  • 9. What is RequireJS? • Modular programming is used to break large applications into smaller blocks of manageable code • Module based coding eases the effort for maintenance and increases reusability. • managing dependencies between modules is a major concern developers face throughout the application development process. • RequireJS is one of the most popular frameworks around for managing dependencies between modules
  • 10. What is RequireJS? • Async Script Loader • 12 k • Very actively developed • Well documented
  • 11. Making code more modular
  • 12. So what's the code look like?
  • 13. Loading from the page <script data-main="js/main" src="scripts/require.js"></script> main.js is the entry point of the app
  • 14. Loading from the page require(["pages/profile"], function(Profile) { ...do stuff... });
  • 15. Defining a Module define(["foo", "baz", "x"], function(Foo, Baz) { ...do stuff... return { init: function(data) { container = data; }, do_something: some_internal_method } });
  • 16. Let's look at an example

Editor's Notes

  1. the language sucks front end scripting will always suck. You're just deling with less predictability and more
  2. your files external plugins where does stuff go? multiple versions of jquery / plugins
  3. it gets monolythic
  4. if you DO refactor, you pay the price. Also, don't move imports to bottom of page - this is a hack.  It
  5. talk about loading