SlideShare une entreprise Scribd logo
1  sur  36
Télécharger pour lire hors ligne
Dependency Management
    with RequireJS
      http://requirejs.org/
Who ?
Julien Cabanès
Front End Developper
mail : julien@zeeagency.com
twitter : @JulienCabanes
github : github.com/ZeeAgency
What are we talkin’ about?

• RequireJS.org : Script Module Loader
• Browsers && Node && Rhino
• Dependencies
• Global Pollution
• Optimization
Context
Libs of choice

  jQuery


 Backbone
Context
Libs of choice   Plugins

  jQuery


 Backbone
Context
Libs of choice   Plugins   Your Code

  jQuery


 Backbone
Bad Context
Libs of choice     Plugins   Your Code

  jQuery


 Backbone
Good Context
Libs of choice   Plugins   Your Code

  jQuery


 Backbone
Dependencies
Libs of choice     Plugins   Your Code

  jQuery


 Backbone
Solution ?
<script   src="jquery.min.js"></script>
<script   src="backbone.min.js"></script>
...
<script   src="jquery.plugin.js"></script>
<script   src="jquery.plugin.js"></script>
...
<script   src="my-code.js"></script>
<script   src="my-code.js"></script>
<script   src="my-code.js"></script>
<script   src="my-code.js"></script>
<script   src="my-code.js"></script>
...
Async Solution ?
<script src="LAB.js"></script>
<script type="text/javascript">
$LAB
	 .script('jquery.min.js')
	 .script('backbone.min.js')
	 ...
	 .script('jquery.plugin.js')
	 .script('jquery.plugin.js')
	 .script('jquery.plugin.js')
	 ...
	 .script('my-code.js')
	 .script('my-code.js')
	 .script('my-code.js');
</script>
Namespace ?
var MyNamespace = {};

MyNamespace.Config = {…};

MyNamespace.Product = function() {…};

MyNamespace.Video = function() {…};

MyNamespace.Audio = function() {…};

MyNamespace.Mail = function() {…};
AMD

Not your CPU...
Asynchronous Module Definition

https://github.com/amdjs/amdjs-api/wiki/AMD
define()
API
define(id?, dependencies?, factory);


Usage
define('My-Module', ['Another-Module'],
	 function(AnotherModule) {
	 	 // Do Something

	 }
);
Example

// App/Conf.js
define(function() {
	 return {
	 	 path: '...',
	 	 debug: true,
	 	 ...
	 };
});
Example

// App/Conf.js
define(function() {
	 return {
	 	 path: '...',
	 	 debug: true,
	 	 ...
	 };
});
Example
// App/Controller/Product.js
define(['App/Conf', 'App/Models/Product', 'App/Views/Product'],
function(Conf, Model, View) {
	
	   // Locale & Private Vars !!!
	   var myPrivateVar = {...};
	
	   // API
	   return {
	   	 show: function(productId) {
	   	
	   	 	 console.log(Conf.path);
	   	 	
	   	 	 var productView = new View();
	   	 	 productView.setModel(Model.getById(productId));
	   	 	
	   	 	 return productView.render();
	   	 },
       ...
	   };
});
Example
// App/Controller/Product.js
define(['App/Conf', 'App/Models/Product', 'App/Views/Product'],
function(Conf, Model, View) {
	
	   // Locale & Private Vars !!!
	   var myPrivateVar = {...};
	
	   // API
	   return {
	   	 show: function(productId) {
	   	
	   	 	 console.log(Conf.path);
	   	 	
	   	 	 var productView = new View();
	   	 	 productView.setModel(Model.getById(productId));
	   	 	
	   	 	 return productView.render();
	   	 },
       ...
	   };
});
Example
// App/Controller/Product.js
define(['App/Conf', 'App/Models/Product', 'App/Views/Product'],
function(Conf, Model, View) {
	
	   // Locale & Private Vars !!!
	   var myPrivateVar = {...};
	
	   // API
	   return {
	   	 show: function(productId) {
	   	
	   	 	 console.log(Conf.path);
	   	 	
	   	 	 var productView = new View();
	   	 	 productView.setModel(Model.getById(productId));
	   	 	
	   	 	 return productView.render();
	   	 },
       ...
	   };
});
Example
// App/Controller/Product.js
define(['App/Conf', 'App/Models/Product', 'App/Views/Product'],
function(Conf, Model, View) {
	
	   // Locale & Private Vars !!!
	   var myPrivateVar = {...};
	
	   // API
	   return {
	   	 show: function(productId) {
	   	
	   	 	 console.log(Conf.path);
	   	 	
	   	 	 var productView = new View();
	   	 	 productView.setModel(Model.getById(productId));
	   	 	
	   	 	 return productView.render();
	   	 },
       ...
	   };
});
Example
// App/Controller/Product.js
define(['App/Conf', 'App/Models/Product', 'App/Views/Product'],
function(Conf, Model, View) {
	
	   // Locale & Private Vars !!!
	   var myPrivateVar = {...};
	
	   // API
	   return {
	   	 show: function(productId) {
	   	
	   	 	 console.log(Conf.path);
	   	 	
	   	 	 var productView = new View();
	   	 	 productView.setModel(Model.getById(productId));
	   	 	
	   	 	 return productView.render();
	   	 },
       ...
	   };
});
Scope vs. Global Pollution
// App/Controller/Product.js
define(['App/Conf', 'App/Models/Product', 'App/Views/Product'],
function(Conf, Model, View) {
	
	   // Locale & Private Vars !!!
	   var myPrivateVar = {...};
	
	   // API
	   return {
	   	 show: function(productId) {
	   	
	   	 	 console.log(Conf.path);
	   	 	
	   	 	 var productView = new View();
	   	 	 productView.setModel(Model.getById(productId));
	   	 	
	   	 	 return productView.render();
	   	 },
...
	   };
});
Plugins Example
// App/View/Product.js
define([
	   'order!FirstModule',
	   'order!SecondModule',
    'i18n!fr/some-i18n-bundle',
	   'text!some-text-file.txt',
    'cs!some-coffee',
	   'tpl!App/View/Product.tpl'
], function(First, Second, bundle, text, coffee, productTpl) {
	
	   // API
	   return Backbone.View.extend({
	
	   	 render: function() {
	   	 	 return productTpl({
	   	 	 	 model: this.model.toJSON()
	   	 	 });
	   	 }
	   });
});
Order Plugin
// App/View/Product.js
define([
    	'order!FirstModule',            // Keep execution order
	    'order!SecondModule',           // requirejs.org/docs/api.html#order
     'i18n!fr/some-i18n-bundle',
	    'text!some-text-file.txt',
     'cs!some-coffee',
	    'tpl!App/View/Product.tpl'
], function(First, Second, bundle, text, coffee, productTpl) {
	
	    // API
	    return Backbone.View.extend({
	
	    	 render: function() {
	    	 	 return productTpl({
	    	 	 	 model: this.model.toJSON()
	    	 	 });
	    	 }
	    });
});
i18n Plugin
// App/View/Product.js
define([
    	'order!FirstModule',
	    'order!SecondModule',
     'i18n!fr/some-i18n-bundle',     // Load i18n bundle
	    'text!some-text-file.txt',      // requirejs.org/docs/api.html#i18n
     'cs!some-coffee',
	    'tpl!App/View/Product.tpl'
], function(First, Second, bundle, text, coffee, productTpl) {
	
	    // API
	    return Backbone.View.extend({
	
	    	 render: function() {
	    	 	 return productTpl({
	    	 	 	 model: this.model.toJSON()
	    	 	 });
	    	 }
	    });
});
Text Plugin
// App/View/Product.js
define([
    	'order!FirstModule',
	    'order!SecondModule',
     'i18n!fr/some-i18n-bundle',
	    'text!some-text-file.txt',      // Guess what ?
     'cs!some-coffee',               // requirejs.org/docs/api.html#text
	    'tpl!App/View/Product.tpl'
], function(First, Second, bundle, text, coffee, productTpl) {
	
	    // API
	    return Backbone.View.extend({
	
	    	 render: function() {
	    	 	 return productTpl({
	    	 	 	 model: this.model.toJSON()
	    	 	 });
	    	 }
	    });
});
CoffeeScript Plugin
// App/View/Product.js
define([
    	'order!FirstModule',
	    'order!SecondModule',
     'i18n!fr/some-i18n-bundle',
	    'text!some-text-file.txt',
     'cs!some-coffee',               // returns compiled CoffeeScript !
	    'tpl!App/View/Product.tpl'      // github.com/jrburke/require-cs
], function(First, Second, bundle, text, coffee, productTpl) {
	
	    // API
	    return Backbone.View.extend({
	
	    	 render: function() {
	    	 	 return productTpl({
	    	 	 	 model: this.model.toJSON()
	    	 	 });
	    	 }
	    });
});
Template Plugin
// App/View/Product.js
define([
    	'order!FirstModule',
	    'order!SecondModule',
     'i18n!fr/some-i18n-bundle',
	    'text!some-text-file.txt',      // My Favorite Plugin ! (mine...)
     'cs!some-coffee',               // Returns a compiled template !
	    'tpl!App/View/Product.tpl'      // github.com/ZeeAgency/requirejs-tpl
], function(First, Second, bundle, text, coffee, productTpl) {
	
	    // API
	    return Backbone.View.extend({
	
	    	 render: function() {
	    	 	 return productTpl({
	    	 	 	 model: this.model.toJSON()
	    	 	 });
	    	 }
	    });
});
Optimization



node r.js -o name=bootstrap out=built.js baseUrl=js



          http://requirejs.org/docs/optimization.html
Remember ?
Libs of choice    Plugins   Your Code

  jQuery


 Backbone
Optimized !
Libs of choice   Ready for Production

  jQuery
Optimized !
Libs of choice   Ready for Production

  jQuery




                   CoffeeScript ?
Optimized !
Libs of choice   Ready for Production

  jQuery




                   CoffeeScript ?
                    Templates ?
Optimized !
Libs of choice       Ready for Production

  jQuery




                     CoffeeScript ?
                      Templates ?
                  Compiled & Minified
           so browser doesn’t need to...
Optimized !
Libs of choice    Ready for Production

  jQuery




                    CoffeeScript ?
                     Templates ?
                 Compiled & Minified
Thx !

Contenu connexe

Tendances

Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator
Alessio Ricco
 

Tendances (20)

Refactoring JavaScript Applications
Refactoring JavaScript ApplicationsRefactoring JavaScript Applications
Refactoring JavaScript Applications
 
CFUGbe talk about Angular JS
CFUGbe talk about Angular JSCFUGbe talk about Angular JS
CFUGbe talk about Angular JS
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
 
Modular and Event-Driven JavaScript
Modular and Event-Driven JavaScriptModular and Event-Driven JavaScript
Modular and Event-Driven JavaScript
 
Arquitetura de Front-end em Aplicações de Larga Escala
Arquitetura de Front-end em Aplicações de Larga EscalaArquitetura de Front-end em Aplicações de Larga Escala
Arquitetura de Front-end em Aplicações de Larga Escala
 
Creating Alloy Widgets
Creating Alloy WidgetsCreating Alloy Widgets
Creating Alloy Widgets
 
Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator
 
Effectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby ConfEffectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby Conf
 
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
 
Intro to AngularJS
Intro to AngularJSIntro to AngularJS
Intro to AngularJS
 
An introduction to Ember.js
An introduction to Ember.jsAn introduction to Ember.js
An introduction to Ember.js
 
How React Native, Appium and me made each other shine @Frontmania 16-11-2018
How React Native, Appium and me made each other shine @Frontmania 16-11-2018How React Native, Appium and me made each other shine @Frontmania 16-11-2018
How React Native, Appium and me made each other shine @Frontmania 16-11-2018
 
Architecture, Auth, and Routing with uiRouter
Architecture, Auth, and Routing with uiRouterArchitecture, Auth, and Routing with uiRouter
Architecture, Auth, and Routing with uiRouter
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
Extend sdk
Extend sdkExtend sdk
Extend sdk
 
Intro to Ember.JS 2016
Intro to Ember.JS 2016Intro to Ember.JS 2016
Intro to Ember.JS 2016
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.js
 
Styling recipes for Angular components
Styling recipes for Angular componentsStyling recipes for Angular components
Styling recipes for Angular components
 
Testing C# and ASP.net using Ruby
Testing C# and ASP.net using RubyTesting C# and ASP.net using Ruby
Testing C# and ASP.net using Ruby
 
IndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceIndexedDB - Querying and Performance
IndexedDB - Querying and Performance
 

Similaire à ParisJS #10 : RequireJS

WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
Spike Brehm
 
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - AppenginePython Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - Appengine
Python Ireland
 
SharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure FunctionsSharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure Functions
Sébastien Levert
 
20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev
Frank Rousseau
 

Similaire à ParisJS #10 : RequireJS (20)

A gently introduction to AngularJS
A gently introduction to AngularJSA gently introduction to AngularJS
A gently introduction to AngularJS
 
Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Api development with rails
Api development with railsApi development with rails
Api development with rails
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSBuilding Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJS
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
Modular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJSModular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJS
 
A Story about AngularJS modularization development
A Story about AngularJS modularization developmentA Story about AngularJS modularization development
A Story about AngularJS modularization development
 
Telerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceTelerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT Conference
 
Angular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersAngular1x and Angular 2 for Beginners
Angular1x and Angular 2 for Beginners
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
 
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - AppenginePython Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - Appengine
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics
 
SharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure FunctionsSharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure Functions
 
Containers & Dependency in Ember.js
Containers & Dependency in Ember.jsContainers & Dependency in Ember.js
Containers & Dependency in Ember.js
 
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java Developers
 
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to PluginDrupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
 
20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

ParisJS #10 : RequireJS

  • 1. Dependency Management with RequireJS http://requirejs.org/
  • 2. Who ? Julien Cabanès Front End Developper mail : julien@zeeagency.com twitter : @JulienCabanes github : github.com/ZeeAgency
  • 3. What are we talkin’ about? • RequireJS.org : Script Module Loader • Browsers && Node && Rhino • Dependencies • Global Pollution • Optimization
  • 4. Context Libs of choice jQuery Backbone
  • 5. Context Libs of choice Plugins jQuery Backbone
  • 6. Context Libs of choice Plugins Your Code jQuery Backbone
  • 7. Bad Context Libs of choice Plugins Your Code jQuery Backbone
  • 8. Good Context Libs of choice Plugins Your Code jQuery Backbone
  • 9. Dependencies Libs of choice Plugins Your Code jQuery Backbone
  • 10. Solution ? <script src="jquery.min.js"></script> <script src="backbone.min.js"></script> ... <script src="jquery.plugin.js"></script> <script src="jquery.plugin.js"></script> ... <script src="my-code.js"></script> <script src="my-code.js"></script> <script src="my-code.js"></script> <script src="my-code.js"></script> <script src="my-code.js"></script> ...
  • 11. Async Solution ? <script src="LAB.js"></script> <script type="text/javascript"> $LAB .script('jquery.min.js') .script('backbone.min.js') ... .script('jquery.plugin.js') .script('jquery.plugin.js') .script('jquery.plugin.js') ... .script('my-code.js') .script('my-code.js') .script('my-code.js'); </script>
  • 12. Namespace ? var MyNamespace = {}; MyNamespace.Config = {…}; MyNamespace.Product = function() {…}; MyNamespace.Video = function() {…}; MyNamespace.Audio = function() {…}; MyNamespace.Mail = function() {…};
  • 13. AMD Not your CPU... Asynchronous Module Definition https://github.com/amdjs/amdjs-api/wiki/AMD
  • 14. define() API define(id?, dependencies?, factory); Usage define('My-Module', ['Another-Module'], function(AnotherModule) { // Do Something } );
  • 15. Example // App/Conf.js define(function() { return { path: '...', debug: true, ... }; });
  • 16. Example // App/Conf.js define(function() { return { path: '...', debug: true, ... }; });
  • 17. Example // App/Controller/Product.js define(['App/Conf', 'App/Models/Product', 'App/Views/Product'], function(Conf, Model, View) { // Locale & Private Vars !!! var myPrivateVar = {...}; // API return { show: function(productId) { console.log(Conf.path); var productView = new View(); productView.setModel(Model.getById(productId)); return productView.render(); }, ... }; });
  • 18. Example // App/Controller/Product.js define(['App/Conf', 'App/Models/Product', 'App/Views/Product'], function(Conf, Model, View) { // Locale & Private Vars !!! var myPrivateVar = {...}; // API return { show: function(productId) { console.log(Conf.path); var productView = new View(); productView.setModel(Model.getById(productId)); return productView.render(); }, ... }; });
  • 19. Example // App/Controller/Product.js define(['App/Conf', 'App/Models/Product', 'App/Views/Product'], function(Conf, Model, View) { // Locale & Private Vars !!! var myPrivateVar = {...}; // API return { show: function(productId) { console.log(Conf.path); var productView = new View(); productView.setModel(Model.getById(productId)); return productView.render(); }, ... }; });
  • 20. Example // App/Controller/Product.js define(['App/Conf', 'App/Models/Product', 'App/Views/Product'], function(Conf, Model, View) { // Locale & Private Vars !!! var myPrivateVar = {...}; // API return { show: function(productId) { console.log(Conf.path); var productView = new View(); productView.setModel(Model.getById(productId)); return productView.render(); }, ... }; });
  • 21. Example // App/Controller/Product.js define(['App/Conf', 'App/Models/Product', 'App/Views/Product'], function(Conf, Model, View) { // Locale & Private Vars !!! var myPrivateVar = {...}; // API return { show: function(productId) { console.log(Conf.path); var productView = new View(); productView.setModel(Model.getById(productId)); return productView.render(); }, ... }; });
  • 22. Scope vs. Global Pollution // App/Controller/Product.js define(['App/Conf', 'App/Models/Product', 'App/Views/Product'], function(Conf, Model, View) { // Locale & Private Vars !!! var myPrivateVar = {...}; // API return { show: function(productId) { console.log(Conf.path); var productView = new View(); productView.setModel(Model.getById(productId)); return productView.render(); }, ... }; });
  • 23. Plugins Example // App/View/Product.js define([ 'order!FirstModule', 'order!SecondModule', 'i18n!fr/some-i18n-bundle', 'text!some-text-file.txt', 'cs!some-coffee', 'tpl!App/View/Product.tpl' ], function(First, Second, bundle, text, coffee, productTpl) { // API return Backbone.View.extend({ render: function() { return productTpl({ model: this.model.toJSON() }); } }); });
  • 24. Order Plugin // App/View/Product.js define([ 'order!FirstModule', // Keep execution order 'order!SecondModule', // requirejs.org/docs/api.html#order 'i18n!fr/some-i18n-bundle', 'text!some-text-file.txt', 'cs!some-coffee', 'tpl!App/View/Product.tpl' ], function(First, Second, bundle, text, coffee, productTpl) { // API return Backbone.View.extend({ render: function() { return productTpl({ model: this.model.toJSON() }); } }); });
  • 25. i18n Plugin // App/View/Product.js define([ 'order!FirstModule', 'order!SecondModule', 'i18n!fr/some-i18n-bundle', // Load i18n bundle 'text!some-text-file.txt', // requirejs.org/docs/api.html#i18n 'cs!some-coffee', 'tpl!App/View/Product.tpl' ], function(First, Second, bundle, text, coffee, productTpl) { // API return Backbone.View.extend({ render: function() { return productTpl({ model: this.model.toJSON() }); } }); });
  • 26. Text Plugin // App/View/Product.js define([ 'order!FirstModule', 'order!SecondModule', 'i18n!fr/some-i18n-bundle', 'text!some-text-file.txt', // Guess what ? 'cs!some-coffee', // requirejs.org/docs/api.html#text 'tpl!App/View/Product.tpl' ], function(First, Second, bundle, text, coffee, productTpl) { // API return Backbone.View.extend({ render: function() { return productTpl({ model: this.model.toJSON() }); } }); });
  • 27. CoffeeScript Plugin // App/View/Product.js define([ 'order!FirstModule', 'order!SecondModule', 'i18n!fr/some-i18n-bundle', 'text!some-text-file.txt', 'cs!some-coffee', // returns compiled CoffeeScript ! 'tpl!App/View/Product.tpl' // github.com/jrburke/require-cs ], function(First, Second, bundle, text, coffee, productTpl) { // API return Backbone.View.extend({ render: function() { return productTpl({ model: this.model.toJSON() }); } }); });
  • 28. Template Plugin // App/View/Product.js define([ 'order!FirstModule', 'order!SecondModule', 'i18n!fr/some-i18n-bundle', 'text!some-text-file.txt', // My Favorite Plugin ! (mine...) 'cs!some-coffee', // Returns a compiled template ! 'tpl!App/View/Product.tpl' // github.com/ZeeAgency/requirejs-tpl ], function(First, Second, bundle, text, coffee, productTpl) { // API return Backbone.View.extend({ render: function() { return productTpl({ model: this.model.toJSON() }); } }); });
  • 29. Optimization node r.js -o name=bootstrap out=built.js baseUrl=js http://requirejs.org/docs/optimization.html
  • 30. Remember ? Libs of choice Plugins Your Code jQuery Backbone
  • 31. Optimized ! Libs of choice Ready for Production jQuery
  • 32. Optimized ! Libs of choice Ready for Production jQuery CoffeeScript ?
  • 33. Optimized ! Libs of choice Ready for Production jQuery CoffeeScript ? Templates ?
  • 34. Optimized ! Libs of choice Ready for Production jQuery CoffeeScript ? Templates ? Compiled & Minified so browser doesn’t need to...
  • 35. Optimized ! Libs of choice Ready for Production jQuery CoffeeScript ? Templates ? Compiled & Minified
  • 36. Thx !