Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
MARIA KERN – NETZ98 GMBH
MAGENTO 2 UI COMPONENTSTHE JS PART
Maria KernSenior Frontend Architect at netz98 GmbH
www.netz98.de
@maja_kern
CommerceHero: maria-kern
Magento 2 UI Components – The JS part
netz98 GmbH www.netz98.de [email protected]
1
2
3
4
5
6
DEFINITION
REAL LIFE EXAMPLEBASIC STRUCTURE
DEBUGGING USEFUL FUNCTIONS AND PROPERTIES
INITIALIZATION PROCESS
MAGENTO 2 UI COMPONENTS – THE JS PART
AGENDA
3
4
UI components definitionMAGENTO 2 UI COMPONENTS – THE JS PART
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
UI COMPONENTS DEFINITION
5
The UI components are:
• the base for many user interface (UI) elements(buttons, fields, tables, minicart)
• designed for simple and flexible rendering of user interfaces
• defined in the module Magento_UI
• used in Magento frontend and backend
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
UI COMPONENTS DEFINITION
6
BACKEND FRONTEND
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
UI COMPONENTS DEFINITION
7
Magento_Ui/js/grid/listing
Magento_Ui/js/grid/toolbar
Magento_Ui/js/form/element/abstract
Magento_Ui/js/form/components/fieldsetBACKEND FRONTEND
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
UI COMPONENTS DEFINITION
8
Magento_Checkout/js/view/cart/totals
Magento_Ui/js/grid/listing
Magento_Ui/js/grid/toolbar
Magento_Ui/js/form/element/abstract
Magento_Checkout/js/view/minicart
Magento_Ui/js/form/components/fieldsetBACKEND FRONTEND
Basic structure of UI componentsMAGENTO 2 UI COMPONENTS – THE JS PART
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
BASIC STRUCTURE OF UI COMPONENTS
10
uiClass
uiElement
uiCollection /uiComponent
JS DEPENDENCY AND INHERITANCE
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
BASIC STRUCTURE OF UI COMPONENTS
11
uiClass
uiElement
uiCollection /uiComponent
JS DEPENDENCY AND INHERITANCE
<UI_Module_dir>/view/base/web/js/lib/core/class.js
<UI_Module_dir>/view/base/web/js/lib/core/element/element.js
<UI_Module_dir>/view/base/web/js/lib/core/collection.js
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
BASIC STRUCTURE OF UI COMPONENTS
12
uiClass
uiElement
uiCollection /uiComponent
JS DEPENDENCY AND INHERITANCE
<UI_Module_dir>/view/base/web/js/lib/core/class.js
<UI_Module_dir>/view/base/web/js/lib/core/element/element.js
<UI_Module_dir>/view/base/web/js/lib/core/collection.js
the uiClass is an abstract class where all components are extended from
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS - THE JS PART
BASIC STRUCTURE OF UI COMPONENTS
13
uiClass
uiElement
uiCollection /uiComponent
custom component 1
custom component 2 custom component 3
child component other child component
JS DEPENDENCY AND INHERITANCE
<UI_Module_dir>/view/base/web/js/lib/core/class.js
<UI_Module_dir>/view/base/web/js/lib/core/element/element.js
<UI_Module_dir>/view/base/web/js/lib/core/collection.js
other child component
use uiElement class as base class of your own component, if you won’t have child components
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS - THE JS PART
BASIC STRUCTURE OF UI COMPONENTS
14
uiClass
uiElement
uiCollection /uiComponent
custom component 1
custom component 2 custom component 3
child component other child component
JS DEPENDENCY AND INHERITANCE
<UI_Module_dir>/view/base/web/js/lib/core/class.js
<UI_Module_dir>/view/base/web/js/lib/core/element/element.js
<UI_Module_dir>/view/base/web/js/lib/core/collection.js
other child component
use uiCollection / uiComponent class as base, if your component will contain a collection of child UI components
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS - THE JS PART
BASIC STRUCTURE OF UI COMPONENTS
15
uiClass
uiElement
uiCollection /uiComponent
custom component 1
custom component 2 custom component 3
child component other child component
JS DEPENDENCY AND INHERITANCE
<UI_Module_dir>/view/base/web/js/lib/core/class.js
<UI_Module_dir>/view/base/web/js/lib/core/element/element.js
<UI_Module_dir>/view/base/web/js/lib/core/collection.js
other child component
Useful functions and propertiesMAGENTO 2 UI COMPONENTS – THE JS PART
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
17
• extend() implements inheritance of UI components
• initialize() is called during instantiation
EXTEND AND INITIALIZE
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
18
• extend() implements inheritance of UI components
• initialize() is called during instantiation
EXTEND AND INITIALIZE
DEPENDENCY
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
19
• extend() implements inheritance of UI components
• initialize() is called during instantiation
EXTEND AND INITIALIZE
INHERITANCE
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
20
• extend() implements inheritance of UI components
• initialize() is called during instantiation
EXTEND AND INITIALIZE
OVERWRITE / EXTEND A METHOD
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
21
CONFIGURATION AND FIRST LEVEL PROPERTIES
Every component has a “defaults” object
> Declaring properties
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
22
CONFIGURATION AND FIRST LEVEL PROPERTIES
custom properties
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
23
CONFIGURATION AND FIRST LEVEL PROPERTIES
a lot of given properties
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
24
CONFIGURATION AND FIRST LEVEL PROPERTIES
Configuration properties will be available as “first level properties”
this.isVisible
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
25
A lot of default properties and methods are already set by the base classes, e.g.:
this.index // name of component
this.name // full name of component including parents
this.template // path to template file
this.component
this.getTemplate()
this.hasTemplate()
CONFIGURATION AND FIRST LEVEL PROPERTIES
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
26
KNOCKOUT JS
Binding data to HTML Elements by data-bind Attribute
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
27
Declaring observable variables
KNOCKOUT JS
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
28
KNOCKOUT JS
Declaring observable variables
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
29
The uiCollection class (mainly) provides a lot of methods for handling of child components
HANDLING OF CHILDREN
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
30
The uiCollection class (mainly) provides a lot of methods for handling of child components
HANDLING OF CHILDREN
Called for every childcomponent
> Child is given as parameter
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
USEFUL FUNCTIONS AND PROPERTIES
31
The uiCollection class (mainly) provides a lot of methods for handling of child components
initElement(child) {}
this.elems()
this.containers
this.getChild(‘nameOfChild’) // UI component
this.hasChild(‘nameOfChild’) // boolean
this.insertChild(‘nameOfComponent’)
HANDLING OF CHILDREN
Initialization processMAGENTO 2 UI COMPONENTS – THE JS PART
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
INITIALIZATION PROCESS
33
All the configurations are translated into json and added to the body
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
INITIALIZATION PROCESS
34
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
INITIALIZATION PROCESS
35
All the configurations are translated into json and added to the body
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
INITIALIZATION PROCESS
36
All the configurations are translated into json and added to the body
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
INITIALIZATION PROCESS
37
• The app calls Magento_Ui/js/core/renderer/layout and passes the UI component’s configuration into the layout
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
INITIALIZATION PROCESS
38
• layout.js creates instances of UI components and its child components
Real life exampleMAGENTO 2 UI COMPONENTS – THE JS PART
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
REAL LIFE EXAMPLE
40
UI component„map-widget“
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
REAL LIFE EXAMPLE
41
UI component„map-widget“
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
REAL LIFE EXAMPLE
42
child UI component„productlist“
productlist could be used in other components as well
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
REAL LIFE EXAMPLE
43
UI ComponentsJS and Template
files
Initialization
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
REAL LIFE EXAMPLE
44
Parent component
Child component
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
REAL LIFE EXAMPLE
45
Parent extendsuiCollection
class
initElement(child) {}this.getChild(‘productlist’)
Options for debuggingMAGENTO 2 UI COMPONENTS – THE JS PART
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
OPTIONS FOR DEBUGGING
47
Chrome Knockoutjs Context Debugger
click on DOM elementinside UI component scope
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
OPTIONS FOR DEBUGGING
48
Magento DevTools by MageSpecialist, with Chrome extension
list of all initialized UI components on the actual page with link to inspect theDOM element
netz98 GmbH www.netz98.de [email protected]
MAGENTO 2 UI COMPONENTS – THE JS PART
CONCLUSION
49
Advantages
• easy teamwork – standardized programming
• common procedures and requirements have already been implemented
• configuring without inline scripting!
Disadvantages
• it takes time to get familiar with
• sometimes it feels to be restricted in frontend development
UI COMPONENTS – THE JS PART
THANK YOU!
References:http://devdocs.magento.com/
Slideshttp://XXXXXXXXXXXXX/