8

In Magento 2 what is the difference between a component and a widget? For example there is a modal component and a modal widget, they both appear to do the same thing.

Component

The Modal UI component implements a secondary window that opens on top of the main window. It uses the modal widget.

Widget

The Magento modal widget implements a secondary window that opens on top of the main window.

The widget can be initialised with a few lines of JS, where as the component requires many lines of XML and still uses the widget. So why would anyone use the XML method?

5

The widget can be initialized with a few lines of JS, where as the component requires many lines of XML and still uses the widget. So why would anyone use the XML method?

The UI component system allows a developer to package up a number of different RequireJS modules as KnockoutJS view model constructors, (with associated "remote" Knockout.js templates), and then use those view models in the HTML source of the Magento page with a special Knockout.js scope binding. Every UI component is also associated with a set of server side data. The end goal is to create a complex piece of UI functionality that's not just one giant messy javascript file, but that end users can include with a simple <uiCompnent name="foo"/> in their layout XML.

So, you're correct that the modal widget is easier to get started with. However, if you're building a complex UI Component, and want your complex UI Component to include modal functionality, you'd pull in Modal UI component.

At least -- that's my current understanding of the situation/system. More context in my UI Component series (self link) if you're interested

  • If i have a task for CRUD something associated with model data example modal form component i should use modal component rather than modal widget! Right ? But seem it more difficult to implement – mrtuvn Sep 22 '16 at 8:28
  • Thanks for the answer and link @alan-storm I'll definitely check them out. It seems much harder to implement but I suppose for a large piece of work it makes more sense. I'll give it a few days for more answers then accept one. – Ben Crook Sep 22 '16 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.