SlideShare a Scribd company logo
Write a Better Java Script 
with RequireJS 
For Reference 
http://requirejs.org
Require js
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
Require js
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

PDF
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, Piacenza
PDF
Applicazioni Web ultra-performanti con Vue.js e Delphi
PDF
Angularjs
PDF
JavaScript
PPTX
AngularJS – Reinventare le applicazioni web
PPTX
Angular js: routing
PPTX
AngularJS: server communication
PPTX
AngularJS 2.0
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, Piacenza
Applicazioni Web ultra-performanti con Vue.js e Delphi
Angularjs
JavaScript
AngularJS – Reinventare le applicazioni web
Angular js: routing
AngularJS: server communication
AngularJS 2.0

What's hot (20)

PDF
AngularJS-Intro
PDF
Sviluppo web con Ruby on Rails
PDF
Workshop angular
PDF
Creare API pubbliche, come evitare gli errori comuni
PDF
jQuery - 1 | WebMaster & WebDesigner
PDF
Zend Framework Workshop Parte1
ODP
Sviluppo applicazioni in Ruby on Rails
PPTX
Slide typescript - net campus
PPTX
CommitUniversity AngularJSAdvanced Andrea Vallotti
PPSX
Powerful asp.net 4 e ie9
PDF
ModulAngular
PDF
Web frameworks
PPT
Zend Framework Workshop Parte2
PDF
Il Web orientato al futuro: Express, Angular e nodeJS
PDF
jQuery - 1 | WebMaster & WebDesigner
PDF
ASP.NET Core - dove siamo arrivati
PPTX
Novità di Asp.Net 4.0
PDF
Workshop Ideare e creare Web Applications, Introduzione ad AngularJS
PDF
Corso WebApp iOS - Lezione 06: Web Development for iOS Devices
ODP
Spring Framework
AngularJS-Intro
Sviluppo web con Ruby on Rails
Workshop angular
Creare API pubbliche, come evitare gli errori comuni
jQuery - 1 | WebMaster & WebDesigner
Zend Framework Workshop Parte1
Sviluppo applicazioni in Ruby on Rails
Slide typescript - net campus
CommitUniversity AngularJSAdvanced Andrea Vallotti
Powerful asp.net 4 e ie9
ModulAngular
Web frameworks
Zend Framework Workshop Parte2
Il Web orientato al futuro: Express, Angular e nodeJS
jQuery - 1 | WebMaster & WebDesigner
ASP.NET Core - dove siamo arrivati
Novità di Asp.Net 4.0
Workshop Ideare e creare Web Applications, Introduzione ad AngularJS
Corso WebApp iOS - Lezione 06: Web Development for iOS Devices
Spring Framework
Ad

Require js

  • 1. Write a Better Java Script with RequireJS For Reference http://requirejs.org
  • 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
  • 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

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