Top Banner
38

Irene Iaccio - Magento2 e RequireJS. The right way

Apr 06, 2017

Download

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Irene Iaccio - Magento2 e RequireJS. The right way
Page 2: Irene Iaccio - Magento2 e RequireJS. The right way

Javascript is a crazy language

Page 3: Irene Iaccio - Magento2 e RequireJS. The right way

TypeError: undefined is not a function

Page 4: Irene Iaccio - Magento2 e RequireJS. The right way

Magento 1

Page 5: Irene Iaccio - Magento2 e RequireJS. The right way

Sorting JS files is a big mess1

Page 6: Irene Iaccio - Magento2 e RequireJS. The right way

Dead scripts2

Page 7: Irene Iaccio - Magento2 e RequireJS. The right way

Wall of code3

Page 8: Irene Iaccio - Magento2 e RequireJS. The right way

RequireJS

Page 9: Irene Iaccio - Magento2 e RequireJS. The right way

NOOOOO! another JavaScript

Library

Page 10: Irene Iaccio - Magento2 e RequireJS. The right way

define(ID?, dependencies?, function)

AMD

Module Definition

Page 11: Irene Iaccio - Magento2 e RequireJS. The right way

define(['jquery', 'underscore'], function($, _) { // Do stuff});

RequireJS

Module Definition

Page 12: Irene Iaccio - Magento2 e RequireJS. The right way

<script data-main="scripts/main" src="scripts/require.js"></script>

Page 13: Irene Iaccio - Magento2 e RequireJS. The right way

// scripts/main.jsrequire(['foo', 'bar'], function(foo) { // Do stuff});

Page 14: Irene Iaccio - Magento2 e RequireJS. The right way

head.appendChild()

Page 15: Irene Iaccio - Magento2 e RequireJS. The right way

Wrapping upRequireJS

http://requirejs.org

Page 16: Irene Iaccio - Magento2 e RequireJS. The right way

Magento 2

Page 17: Irene Iaccio - Magento2 e RequireJS. The right way

Resource optimization

Page 18: Irene Iaccio - Magento2 e RequireJS. The right way

Resource optimization

Page 19: Irene Iaccio - Magento2 e RequireJS. The right way

Resource optimization

Page 20: Irene Iaccio - Magento2 e RequireJS. The right way

var config = { //Settings};

require-config.js

Page 21: Irene Iaccio - Magento2 e RequireJS. The right way

baseUrl

site / static / area / vendor / theme / locale

Page 22: Irene Iaccio - Magento2 e RequireJS. The right way

baseUrl

baseUrl + Vendor_Module/script + .js

Page 23: Irene Iaccio - Magento2 e RequireJS. The right way

Pathsvar config = { paths: { "module-name": "Vendor_Module/script" }};

Page 24: Irene Iaccio - Magento2 e RequireJS. The right way

Map map: { '*': { "menu": "Vendor_Module/js/menu" } }

Page 25: Irene Iaccio - Magento2 e RequireJS. The right way

define([ "jquery"], function ($) { 'use strict';

function awesomeMenu() { return "this is a private function"; } return function () { var message = awesomeMenu(); alert(message); }});

Page 26: Irene Iaccio - Magento2 e RequireJS. The right way

Shim

define([ “jquery”, “jquery-plugin” ], function($) { // do stuff });

Page 27: Irene Iaccio - Magento2 e RequireJS. The right way

shim: {

'jquery-plugin': {

deps: ['jquery']

}

}

Page 28: Irene Iaccio - Magento2 e RequireJS. The right way

Config config: { “module-name”: { key: “value” } }

define([“module”], function (module) { module.config().key});

Page 29: Irene Iaccio - Magento2 e RequireJS. The right way

Mixinsvar config = { 'config':{ 'mixins': { 'target/module': { 'Vendor_Module/script' : true } } }};

Page 30: Irene Iaccio - Magento2 e RequireJS. The right way

Do something between the checkout steps

config: { mixins: { 'Magento_Checkout/js/model/step-navigator': { 'js/checkoutCustomization': true } }}

Page 31: Irene Iaccio - Magento2 e RequireJS. The right way

define([ "jquery" ], function ($) { 'use strict';

return function (target) { target.next = function() { // do stuff }; return target };});

Page 32: Irene Iaccio - Magento2 e RequireJS. The right way

define(['jquery'], function ($) { return function (widget) {

$.widget('mage.SwatchRenderer', widget, {

updateBaseImage: function (images, context, isProductViewExist) { //do stuff }

}); return $.mage.SwatchRenderer; }});

Page 33: Irene Iaccio - Magento2 e RequireJS. The right way

Want more?

Page 34: Irene Iaccio - Magento2 e RequireJS. The right way

Plugins

define([ 'text!mage/gallery/gallery.html',], function (galleryTpl) { // Gallery});

text!

Page 35: Irene Iaccio - Magento2 e RequireJS. The right way

Plugins

define(['domReady!'], function (doc) { //This function is called //once the DOM is ready});

domReady!

Page 36: Irene Iaccio - Magento2 e RequireJS. The right way

Benefits1. Asynchronous module loading, deferred execution.

2. More clean and maintainable code.

3. Code & data sharing between different modules.

4. Global variables avoidance.

Page 37: Irene Iaccio - Magento2 e RequireJS. The right way

Write modular code!

Page 38: Irene Iaccio - Magento2 e RequireJS. The right way

“Using a modular script loader like RequireJS will improve the speed and quality of your code."

Questions?@Nuovecode github.com/nuovecode