What is requireJS AMD in Magento2

Updated: Aug 20, 2020

Simply we can say, below format is the AMD module.

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

Module Vs AMD module

1. Javascript Module Pattern

(function () { this.myGlobal = function () {}; }());

In the javascript module pattern, the dependencies are assumed to be immediately available when this function executes. This limits the loading strategies for the dependencies. AMD module addresses these issues by the below way.

2. AMD Module

//Calling define with a dependency array and a factory function define(['dep1', 'dep2'], function (dep1, dep2) { //Define the module value by returning a value. return function () {}; });

  1. Register the factory function by calling define(), instead of immediately executing it.

  2. Pass dependencies as an array of string values, do not grab globals.

  3. Only execute the factory function once all the dependencies have been loaded and executed.

  4. Pass the dependent modules as arguments to the factory function.

I hope it helps to grab the idea of what is AMD in Magento 2.

#amd #javascript #magento2 #requirejs

15 views0 comments

Recent Posts

See All

Magento 2 CMS block with the same ID already exists

I have seen the below error when I open cms block grid something went wrong with processing the default view and we have restored the filter to its original state. I found the below log in the excepti

How to generate bearer token - Magento 2 REST API

We can generate the admin bearer token using the below command. Please run the below command in the terminal to generate a token. curl -XPOST -H 'Content-Type: application/json' http://www.domain.com/

Magento 2 script tag and available options in layout xml

We can add a script via layout XML file in Magento 2. The below format will stop the HTML parse until js downloaded and executed. <script src="Bilal_Usean/js/sample.min.js" /> The below format will st