Table of Contents for
Magento 2 Development Quick Start Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Magento 2 Development Quick Start Guide by Branko Ajzele Published by Packt Publishing, 2018
  1. Magento 2 Development Quick Start Guide
  2. Title Page
  3. Copyright and Credits
  4. Magento 2 Development Quick Start Guide
  5. Packt Upsell
  6. Why subscribe?
  7. Packt.com
  8. Contributors
  9. About the author
  10. About the reviewer
  11. Packt is searching for authors like you
  12. Table of Contents
  13. Preface
  14. Who this book is for
  15. What this book covers
  16. To get the most out of this book
  17. Download the example code files
  18. Code in Action
  19. Conventions used
  20. Get in touch
  21. Reviews
  22. Understanding the Magento Architecture
  23. Technical requirements
  24. Installing Magento
  25. Modes
  26. Areas
  27. Request flow processing
  28. Modules
  29. Creating the minimal module
  30. Cache
  31. Dependency injection
  32. Argument injection
  33. Virtual types
  34. Proxies
  35. Factories
  36. Plugins
  37. The before plugin
  38. The around plugin
  39. The after plugin
  40. Events and observers
  41. Console commands
  42. Cron jobs
  43. Summary
  44. Working with Entities
  45. Technical requirements
  46. Understanding types of models
  47. Creating a simple model
  48. Methods worth memorizing
  49. Working with setup scripts
  50. The InstallSchema script
  51. The UpgradeSchema script
  52. The Recurring script
  53. The InstallData script
  54. The UpgradeData script
  55. The RecurringData script
  56. Extending entities
  57. Creating extension attributes
  58. Summary
  59. Understanding Web APIs
  60. Technical requirements
  61. Types of users
  62. Types of authentication
  63. Types of APIs
  64. Using existing web APIs
  65. Creating custom web APIs
  66. Understanding search criteria
  67. Summary
  68. Building and Distributing Extensions
  69. Technical requirements
  70. Building a shipping extension
  71. Distributing via GitHub
  72. Distributing via Packagist
  73. Summary
  74. Developing for Admin
  75. Technical requirements
  76. Using the listing component
  77. Using the form component
  78. Summary
  79. Developing for Storefront
  80. Technical requirements
  81. Setting up the playground
  82. Calling and initializing JS components
  83. Meet RequireJS
  84. Replacing jQuery widget components
  85. Extending jQuery widget components
  86. Creating jQuery widgets components
  87. Creating UI/KnockoutJS components
  88. Extending UI/KnockoutJS components
  89. Summary
  90. Customizing Catalog Behavior
  91. Technical requirements
  92. Creating the size guide
  93. Creating the same day delivery
  94. Flagging new products
  95. Summary
  96. Customizing Checkout Experiences
  97. Technical requirements
  98. Passing data to the checkout
  99. Adding order notes to the checkout
  100. Summary
  101. Customizing Customer Interactions
  102. Technical requirements
  103. Understanding the section mechanism
  104. Adding contact preferences to customer accounts
  105. Adding contact preferences to the checkout
  106. Summary
  107. Other Books You May Enjoy
  108. Leave a review - let other readers know what you think

Meet RequireJS

To this point, we have been using things like redirectUrl and cookieNotices out of thin air, but how exactly do these components become available to our code? The answer is, via RequireJS, a library that underlies nearly every other JS feature built into Magento. The overall role of RequireJS is simple; it is a JS module system that implements the Asynchronous Module Definition (AMD) standard, which serves as an improvement over the web's current globals and script tags.

We have already seen the format of these AMD modules in the preceding examples, which comes down the following:

define(['dep1', 'dep2'], function (dep1, dep2) {
return function () {
// Module value to return
};
});

The gist of  AMD modules functionality comes down to each module being able to:

  • Register the factory function via define
  • Inject dependencies, instead of using globals
  • Execute the factory function when all dependencies become accessible
  • Pass dependent modules as arguments to the factory function

This strategy solves many of the conventional dependency issues, where dependencies are assumed to be immediately available when the function executes, which is not always the case.

If we were to do a View Page Source on our Playground page in a browser, we would see three <script type="text/javascript" src="..."> tags with their src attributes pointing to the following JS files:

frontend/Magento/luma/en_US/requirejs/require.js
frontend/Magento/luma/en_US/mage/requirejs/mixins.js
frontend/Magento/luma/en_US/requirejs-config.js

A quick look at the partial requirejs-config.js file reveals how these get loaded:

(function (require) {
/* ... */
(function () {
var config = {
map: {
'*': {
'redirectUrl': 'mage/redirect-url',
}
}
};
require.config(config);
})();
/* ... */
(function () {
var config = {
map: {
'*': {
cookieNotices: 'Magento_Cookie/js/notices'
}
}
};
require.config(config);
})();
/* ... */
})(require);

These two mappings break down as follows: 

  • The left-hand side points to the freely given name of our JS component, which essentially tells consumers how to reference it. This is why we were able to use these two components simply by referencing them via redirectUrl and cookieNotices.
  • The right-hand side points to the location of our JS component:
    • mage/redirect-url, where mage points to the <PROJECT_DIR>/lib/web/mage directory, and redirect-url further points to the redirect-url.js file within that directory
    • Magento_Cookie/js/notices, where Magento_Cookie points to the <MAGENTO_DIR>/module-cookie/view/frontend/web directory, and js/notices further points to the js/notices.js file within that directory

Further observing the requirejs-config.js file, aside from map, there are a few other important keys whose roles are worth knowing:

var config = {
map: {
'*': {
/* ... */
}
},
paths: {
/* ... */
},
shim: {
/* ... */
},
deps: [
/* ... */
],
config: {
mixins: {
/* ... */
}
}
};

These break down as follows:

  • map: For the given module prefix; instead of loading the module with the given ID, substitute a different module ID
  • paths: Path mappings for module names not found directly under baseUrl
  • shim: Configure the dependencies, exports, and custom initialization for older browser globals scripts that do not use define for declaring the dependencies and setting the module value
  • deps: An array of dependencies to load
  • config/mixins: List of JS class mappings, for classes whose methods are added to, or mixed in, with other JS classes
See https://requirejs.org/docs/api.html for more information on the RequireJS API.

The takeaway here is that our own modules can define the requirejs-config.js file on their own, under the <MODULE_DIR>/view/frontend directory, allowing us to hook into the final Magento requirejs-config.js file that gets generated for the browser. This, in turn, allows us to easily register our own components, override existing mappings, paths, and other things.