SlideShare a Scribd company logo
1 of 38
Download to read offline
Javascript is a
crazy language
TypeError: undefined is not a function
Magento 1
Sorting JS files is a big mess1
Dead scripts2
Wall of code3
RequireJS
NOOOOO!
another JavaScript
Library
define(ID?, dependencies?, function)
AMD
Module Definition
define(['jquery', 'underscore'], function($, _) {
// Do stuff
});
RequireJS
Module Definition
<script data-main="scripts/main" src="scripts/require.js"></script>
// scripts/main.js
require(['foo', 'bar'], function(foo) {
// Do stuff
});
head.appendChild()
Wrapping up
RequireJS
http://requirejs.org
Magento 2
Resource optimization
Resource optimization
Resource optimization
var config = {
//Settings
};
require-config.js
baseUrl
site / static / area / vendor / theme / locale
baseUrl
baseUrl + Vendor_Module/script + .js
Paths
var config = {
paths: {
"module-name": "Vendor_Module/script"
}
};
Map
map: {
'*': {
"menu": "Vendor_Module/js/menu"
}
}
define([
"jquery"
], function ($) {
'use strict';
function awesomeMenu() {
return "this is a private function";
}
return function () {
var message = awesomeMenu();
alert(message);
}
});
Shim
define([ “jquery”, “jquery-plugin” ], function($) {
// do stuff
});
shim: {
'jquery-plugin': {
deps: ['jquery']
}
}
Config
config: {
“module-name”: {
key: “value”
}
}
define([“module”], function (module) {
module.config().key
});
Mixins
var config = {
'config':{
'mixins': {
'target/module': {
'Vendor_Module/script' : true
}
}
}
};
Do something between the checkout steps
config: {
mixins: {
'Magento_Checkout/js/model/step-navigator': {
'js/checkoutCustomization': true
}
}
}
define([ "jquery" ], function ($) {
'use strict';
return function (target) {
target.next = function() {
// do stuff
};
return target
};
});
define(['jquery'], function ($) {
return function (widget) {
$.widget('mage.SwatchRenderer', widget, {
updateBaseImage: function (images, context, isProductViewExist) {
//do stuff
}
});
return $.mage.SwatchRenderer;
}
});
Want more?
Plugins
define([
'text!mage/gallery/gallery.html',
], function (galleryTpl) {
// Gallery
});
text!
Plugins
define(['domReady!'], function (doc) {
//This function is called
//once the DOM is ready
});
domReady!
Benefits
1. Asynchronous module loading, deferred execution.
2. More clean and maintainable code.
3. Code & data sharing between different modules.
4. Global variables avoidance.
Write modular
code!
“Using a modular script loader like RequireJS will
improve the speed and quality of your code."
Questions?
@Nuovecode github.com/nuovecode

More Related Content

What's hot

BPA 2012: Web Application Project
BPA 2012: Web Application ProjectBPA 2012: Web Application Project
BPA 2012: Web Application Project
FrenchRSA
 
HTML5 and Mobile
HTML5 and MobileHTML5 and Mobile
HTML5 and Mobile
doodoofish
 
PostgreSQL (2) by Aswin
PostgreSQL (2) by AswinPostgreSQL (2) by Aswin
PostgreSQL (2) by Aswin
Agate Studio
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and Modernizr
Jussi Pohjolainen
 

What's hot (19)

NoSQL - Hands on
NoSQL - Hands onNoSQL - Hands on
NoSQL - Hands on
 
Coisas para o blog
Coisas para o blogCoisas para o blog
Coisas para o blog
 
JBoss Seam vs JSF
JBoss Seam vs JSFJBoss Seam vs JSF
JBoss Seam vs JSF
 
2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach
 
Running ms sql stored procedures in mule
Running ms sql stored procedures in muleRunning ms sql stored procedures in mule
Running ms sql stored procedures in mule
 
webstudy jquery
webstudy jquerywebstudy jquery
webstudy jquery
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS Overview
 
Hack tutorial
Hack tutorialHack tutorial
Hack tutorial
 
WebsiteStructure
WebsiteStructureWebsiteStructure
WebsiteStructure
 
BPA 2012: Web Application Project
BPA 2012: Web Application ProjectBPA 2012: Web Application Project
BPA 2012: Web Application Project
 
Writing Drupal 5 Module
Writing Drupal 5 ModuleWriting Drupal 5 Module
Writing Drupal 5 Module
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
 
HTML5 and Mobile
HTML5 and MobileHTML5 and Mobile
HTML5 and Mobile
 
An Introduction to Drupal
An Introduction to DrupalAn Introduction to Drupal
An Introduction to Drupal
 
PostgreSQL (2) by Aswin
PostgreSQL (2) by AswinPostgreSQL (2) by Aswin
PostgreSQL (2) by Aswin
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and Modernizr
 
лукьянченко л.а. пос 10а
лукьянченко л.а. пос 10алукьянченко л.а. пос 10а
лукьянченко л.а. пос 10а
 
2012-03-20 - Getting started with Node.js and MongoDB on MS Azure
2012-03-20 - Getting started with Node.js and MongoDB on MS Azure2012-03-20 - Getting started with Node.js and MongoDB on MS Azure
2012-03-20 - Getting started with Node.js and MongoDB on MS Azure
 

Viewers also liked

Viewers also liked (8)

Magento 2.2 B2B, Pimcore, Open Loyalty - features and case study
Magento 2.2 B2B, Pimcore, Open Loyalty -  features and case studyMagento 2.2 B2B, Pimcore, Open Loyalty -  features and case study
Magento 2.2 B2B, Pimcore, Open Loyalty - features and case study
 
David Bolufer - Make your magento2 fly2
David Bolufer - Make your magento2 fly2David Bolufer - Make your magento2 fly2
David Bolufer - Make your magento2 fly2
 
How to build a container monitoring solution - David Gildeh, CEO and Co-Found...
How to build a container monitoring solution - David Gildeh, CEO and Co-Found...How to build a container monitoring solution - David Gildeh, CEO and Co-Found...
How to build a container monitoring solution - David Gildeh, CEO and Co-Found...
 
vue-storefront - PWA eCommerce for Magento2 MM17NYC presentation
vue-storefront - PWA eCommerce for Magento2 MM17NYC presentationvue-storefront - PWA eCommerce for Magento2 MM17NYC presentation
vue-storefront - PWA eCommerce for Magento2 MM17NYC presentation
 
Magento2 Search insights by Klevu
Magento2 Search insights by KlevuMagento2 Search insights by Klevu
Magento2 Search insights by Klevu
 
Magento2 Varnish Integration | Magento2 Speed & Optimization
Magento2 Varnish Integration | Magento2 Speed & OptimizationMagento2 Varnish Integration | Magento2 Speed & Optimization
Magento2 Varnish Integration | Magento2 Speed & Optimization
 
Tools out of the box with Magento 2 in PHPSTORM
Tools out of the box with Magento 2 in PHPSTORMTools out of the box with Magento 2 in PHPSTORM
Tools out of the box with Magento 2 in PHPSTORM
 
Applying Code Customizations to Magento 2
Applying Code Customizations to Magento 2 Applying Code Customizations to Magento 2
Applying Code Customizations to Magento 2
 

Similar to Require js and Magento2

Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJS
Aaronius
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 

Similar to Require js and Magento2 (20)

Managing JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJSManaging JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJS
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJS
 
SPTechCon Boston 2015 - Whither SPServices?
SPTechCon Boston 2015 - Whither SPServices?SPTechCon Boston 2015 - Whither SPServices?
SPTechCon Boston 2015 - Whither SPServices?
 
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptjQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
 
JavaScript Modules in Practice
JavaScript Modules in PracticeJavaScript Modules in Practice
JavaScript Modules in Practice
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Beyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance JavascriptBeyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance Javascript
 
jQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends ForeverjQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends Forever
 
RequireJS
RequireJSRequireJS
RequireJS
 
All you need to know about JavaScript loading and execution in the browser - ...
All you need to know about JavaScript loading and execution in the browser - ...All you need to know about JavaScript loading and execution in the browser - ...
All you need to know about JavaScript loading and execution in the browser - ...
 
Zend Framework 2 - Basic Components
Zend Framework 2  - Basic ComponentsZend Framework 2  - Basic Components
Zend Framework 2 - Basic Components
 
Sqladria 2009 SRC
Sqladria 2009 SRCSqladria 2009 SRC
Sqladria 2009 SRC
 
Doctrine for NoSQL
Doctrine for NoSQLDoctrine for NoSQL
Doctrine for NoSQL
 
Doctrine and NoSQL
Doctrine and NoSQLDoctrine and NoSQL
Doctrine and NoSQL
 
Web internship Yii Framework
Web internship  Yii FrameworkWeb internship  Yii Framework
Web internship Yii Framework
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
前端概述
前端概述前端概述
前端概述
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 

Recently uploaded

TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 

Recently uploaded (20)

ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 

Require js and Magento2