jQuery widget coding standard

All Adobe Commerce and Magento Open Source jQuery UI widgets and interactions are built on a simple, reusable base---the jQuery UI Widget Factory.

The factory provides a flexible base for building complex, stateful plug-ins with a consistent API. It is designed not only for plug-ins that are part of jQuery UI, but for general usage by developers who want to create object-oriented components without reinventing common infrastructure.

For more information, see the jQuery Widget API documentation.

This standard is mandatory for core developers and recommended for third-party extension developers. Some parts of the code might not comply with the standard, but we are working to gradually improve this.

Use RFC 2119 to interpret the "must," "must not," "required," "shall," "shall not," "should," "should not," "recommended," "may," and "optional" keywords.

Naming conventions

Instantiation and resources

Initializing a component on a selector

There are two ways to initialize a component on a selector:

<div id="element-id" data-mage-init='{"Vendor_Module/js/myfile":{}}'></div>
<script type="text/x-magento-init">
{
   "#element-id": {
       "Vendor_Module/js/myfile": {}
   }
}
</script>

In these cases the path to the file is:

Vendor/Module/view/frontend/web/js/jsfilename.js

which contains your code:

define(['uiComponent'],
  function (Component) {
    'use strict';
    return Component.extend({
      initialize: function (config, node) {
        // some code
      }
    });
  });

Initializing a component on a selector with parameters

When a component is initialized, it is also important to send parameters to it, which are normally determined dynamically in PHP.

<div id="element-id" data-mage-init='{"Vendor_Module/js/myfile":{"parameter":"value","status":<?php echo $block->getStatus(); ?>
}}'></div>
<script type="text/x-magento-init">
{
   "#element-id": {
       "Vendor_Module/js/myfile1": {
           "parameter":"value",
           "status":<?php echo $block->getStatus(); ?>
       }
   }
}
</script>

Development standards

Architecture