Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Taming the JavaScript Beast in Magento 2

Max Bucknell
September 19, 2016

Taming the JavaScript Beast in Magento 2

Presentation given by Max Bucknell and Marcin Szterling at Meet Magento Poland 2016, 2016-09-19.

Max Bucknell

September 19, 2016
Tweet

More Decks by Max Bucknell

Other Decks in Programming

Transcript

  1. Max Bucknell & Marcin Szterling Taming the JavaScript Beast in

    Magento 2 Ujarzmić JavaScript w Magento 2
  2. Take a Deep Breath • What's Changed for M2? •

    RequireJS • JavaScript Components • jQuery UI Widgets • UI Apps
  3. What's Changed? • No more Prototype • No more inline

    scripts • No more adding JS to the <head> • No more global variables • And a bunch of new stuff
  4. RequireJS • A JavaScript module loader • 1.0 in 2011

    • Loads JavaScript on a page. • Loads a "module", and its dependencies, and its dependencies, and so on.
  5. AMD • Not a chip manufacturer • Stands for "Asynchronous

    Module Definition" • The format of RequireJS "modules" • Used in all Magento 2 JavaScript files
  6. Module Definition • To define an AMD module, you write

    a function • This function will be called once when your module is loaded • Whatever you return is what dependents get, your public API
  7. RequireJS – Impacts • All JavaScript is loaded after your

    content • Separation of concerns • No global scope pollution
  8. AMD Dependencies • How does RequireJS know what jquery means?

    • ...or collapsible? • ...or mage/translate? • ...or Magento_Checkout/js/view/sidebar?
  9. AMD Dependencies • Modules are referred to by their name,

    which comes from the URL: store.biz/static/frontend/Magento/luma/pl_PL/Magent
  10. AMD Dependencies • Modules are referred to by their name,

    which comes from the URL: PL/Magento_Checkout/js/view/sidebar
  11. AMD Dependencies • Can also be configured via a mapping

    • requirejs-config.js • As a sibling to the web directory • Standard RequireJS Config • Merged by Magento\Framework\Requirejs
  12. JavaScript Component • Basic unit of functionality in Magento 2

    • Built on top of AMD, loaded by RequireJS • Gives us The Way to get JavaScript into a page • Comes in two parts: Definition, and Declaration
  13. JS Component Definition • A normal AMD module that returns

    a function. • Function takes parameters of a configuration object, and an HTML element.
  14. JS Component Definition define([], function addClass() { function main (config,

    el) { el.classList.add( config.className); } return main; });
  15. JS Component Declaration • JavaScript is included in the template,

    not the layout XML • Upshot: JavaScript is only included when it needs to be • Specially formatted JSON, that is picked up by Magento
  16. JS Component Definition define([], function addClass() { function main (config,

    el) { el.classList.add( config.className); } return main; });
  17. JS Components • JS Components will only load once, but

    may be run many times. • Convention comes from jQuery UI: All jQuery UI Widgets are valid JS Components. • To run some general JavaScript, use * as the element selector.
  18. UI App • A UI App (defined in Magento_Ui) is

    a JavaScript Component. • Renders a tree of uiComponents into a dynamic web app. • uiComponents ≠ UI Components
  19. UI Components? • UI Components are UI Apps under the

    hood. • The checkout is not a UI Component, it is a UI App. • Configured by the layout, not a ui_component file. • UI Components are out of scope.
  20. uiComponent • AMD Module. • A view model, or "block"

    in Magento language. • Extend from this root component to create your own. • Define the public API for a template.
  21. When to Use • For rendering private data. • Serves

    whole page from Varnish. • Only loads private data through API.
  22. uiComponent define([ 'uiComponent', ], function myComponent ( Component ) {

    return Component.extend({ // Public API }); });
  23. Templating • Your view-model is linked to a template. •

    Templates are parsed by Knockout • Knockout uses a special binding syntax to make views dynamic.
  24. Observables • Your view model will receive notifications about state

    changes. • Your view needs to respond to these. • Knockout Observables are data objects you can "subscribe" to, and be notified of changes.
  25. Component Configuration • uiComponents are configured in the layout XML

    as a constructor argument. • Syntax is like DI XML, with deeply nested arrays. • Magento\Checkout\Block\Onepage resolves this array into JSON.
  26. Rendering Children • An app is a tree of UI

    Components. • Get a child by calling getRegion(name). • getRegion always returns an array. • Get all children with this.regions.
  27. Rendering a child <!-- ko foreach: getRegion('name') --> <!-- ko

    template: getTemplate() --> <!-- /ko --> <!-- /ko -->
  28. Component Registry • A component "class" is a function that

    returns a constructor, not the component itself. • You cannot load a component as a dependency • Use the registry to get a component. • Registry key is the component's name with all parent component's names. Not displayArea
  29. Component Registry • All JavaScript is loaded and executed asynchronously.

    • You cannot depend on components being loaded. • Magento has an answer here...
  30. Conclusion • This is quite a comprehensive framework for web

    apps. • It's new. Expect issues. Some common things are hard or ugly.
  31. ?