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

Creating UI/KnockoutJS components

To this point, we have only been dealing with jQuery widgets as components. While extremely powerful, jQuery widgets are not best suited for rendering robust components with complex HTML structures. The other type of JS components is what we refer to as UI/KnockoutJS components. Built on the shoulders of the KnockoutJS library, these components allow powerful templating of our data, among other things. Without getting too deep into the ins and outs of these type of components, suffice to say that the main construct we are referring to when we speak of UI/KnockoutJS components is uiComponent.

As per <MAGENTO_DIR>/module-ui/view/base/requirejs-config.js, the uiComponent maps to the Magento_Ui/js/lib/core/collection JS file. Inspecting the collection.js file, we can see that uiComponent extends uiElement, which maps to the Magento_Ui/js/lib/core/element/element JS file. The uiComponent and uiElement make use of the ko, underscore, mageUtils, uiRegistry, uiEventsand uiClass libraries, among other things, so it's worth getting ourselves familiar with those.

Creating new UI/KnockoutJS components is a slightly more involved process than creating a jQuery widget.

We start by creating the proper mapping under our <MODULE_DIR>/view/frontend/requirejs-config.js file, as follows:

var config = {
map: {
'*': {
popularProducts: 'Magelicious_Jsco/js/popular-products'
}
}
};

This part is the same as with jQuery widgets. Here we simply register, or alias if you will, our component name to its file location. 

We then define the component itself, under the <MODULE_DIR>/view/frontend/web/js/popular-products.js file, as follows:

define([
'jquery',
'uiComponent',
'ko',
'mage/translate'
], function ($, Component, ko, $t) {
'use strict';
return Component.extend({
defaults: {
template: 'Magelicious_Jsco/popular-products',
title: $t('Popular Products'),
products: [],
},
getTitle: function () {
return this.title;
}
});
}
);

The basis of all UI components is uiComponent. We pass on the instance of uiComponent as a Component parameter. We then implement the specifics of our component as part of the  JSON object passed onto the Component.extend method.

With our component JS file now in place, we further create the template file referenced by the component. We do so under the <MODULE_DIR>/view/frontend/web/template/popular-products.html file, as follows:

<h4 data-bind="text: getTitle()"></h4>
<ul data-bind="foreach: products">
<li>
<span>
<span data-bind="text: title"></span>
(<span data-bind="text: sku"></span>)
</span>
</li>
</ul>

What happens in the HTML template files is all about KnockoutJS, which means a certain part of the KnockoutJS library is required in order to built UI/KnockoutJS components.

See http://knockoutjs.com for more information on the KnockoutJS library.

We then amend our jsco_playground_index.xml by adding the following line under <referenceContainer name="content">:

<block name="popular_products" 
template="Magelicious_Jsco::popular-products.phtml" />

popular-products.phtml is where we will instantiate our UI/KnockoutJS component.

Finally, we create <MODULE_DIR>/view/frontend/templates/popular-products.phtml with content, as follows:

<?php $jsonHelper = $this->helper('Magento\Framework\Json\Helper\Data'); ?>

<div class="popular-products" data-bind="scope:'popular-products-scope'">
<!-- ko template: getTemplate() --><!-- /ko -->
</div>

<script type="text/x-magento-init">
{
".popular-products": {
"Magento_Ui/js/core/app": {
"components": {
"popular-products-scope": {
"component": "popularProducts",
"products": <?= /* @escapeNotVerified */ $jsonHelper->jsonEncode([
['sku' => 'sku1', 'title' => 'Title1'],
['sku' => 'sku2', 'title' => 'Title2']
]) ?>
}
}
}
}
}
</script>

Here we are using the declarative approach to initialize our component. The structure of the JSON object under the script tag might seem a bit confusing at first. The .popular-products key is essentially a selector, targeting whatever HTML element it might find. Magento_Ui/js/core/app is an alias for the  app.js file, which creates the UI components instances according to the configuration of the JSON using  theuiLayout component. components is a key under which we nest one or more components we wish to initialize. popular-products-scope is sort of a scope key assigned to our component, which we use to data-bind the scope value to the HTML element.

Clearing the cache and redeploying the static files, we should now be able to see our newly created component.