# Magewire - Features
## Index
- [Best Practices](#best-practices)
- [Folder Structure](#folder-structure)
- [Javascript](#javascript)
- [Document Events](#document-events)
- [Lifecycle Hooks](#lifecycle-hooks)
- [Block Structure](#block-structure)
- [Dynamic Components](#dynamic-components)
- [Blocks](#blocks--dc-)
- [Widgets](#widgets--dc-)
- [Templates](#templates)
- [Switch Template](#switch-template)
- [Directives](#directives)
- [Wire Ignore](#wire-ignore)
- [Children Block Rendering](#child-block-rendering)
- [Wire Select](#wire-select)
- [Component Types](#component-types)
- [Magic Actions & Properties](#magic-actions--properties)
- [Overwrites](#overwrites)
- [Properties](#properties)
- [Name Specific](#name-specific)
- [Nesting](#nesting)
- [Flash Messages](#flash-messages)
- [Redirects](#redirects)
- [Listeners & Emits](#listeners--emits)
- [Javascript](#javascript--el-)
- [Magic Actions Compatibility](#magic-actions-compatibility)
- [Global Refresh Listener](#global-refresh-listener)
- [Lifecycle Hooks](#lifecycle-hooks)
- [Browser Events](#browser-events)
- [Prefetching](#prefetching)
- [Lazy Updating](#lazy-updating)
- [Keydown Modifiers](#keydown-modifiers)
- [Restricted Public Methods](#restricted-public-methods)
- [Global](#global-rpm)
- [Component](#component-rpm)
- [Pagination](#pagination)
- [Query String](#query-string)
- [Display Loading State](#display-loading-state)
- [Indicator Customization](#indicator-customization)
- [Indicator Removal](#indicator-removal)
- [Custom Example](#custom-example-dls)
- [Notification Type Styling](#notification-type-styling)
- [Plugins](#plugins)
- [Plugin: Loader](#plugin--loader)
- [Loader System Settings](#loader-system-settings)
- [Plugin: Error](#plugin--error)
- [Custom onError callback](#custom-onerror-callback)
- [Component Resolvers](#component-resolvers)
- [Custom Resolvers](#custom-resolver)
- [Reset](#reset)
- [Forms](#forms)
- [Message Translations](#message-translations--f-)
- [Message Displayment](#message-displayment--f-)
- [Example 1](#example-1--md-)
- [Example 2](#example-2--md-)
- [Example 3](#example-3--md-)
- [Example 4](#example-4--md-)
## Best Practices
1. Use the Magewire naming conventions and structures.
2. Use Hydrators to manipulate data before or after a method gets called.
3. Contribute or create an issue when you found bugs or sucurity issues.
4. Keep components small and clean.
5. Use the ```My/Module/Magewire/Component``` folder when you introduce a new component type.
6. The ```$magewire``` component object is available in the template by default.
## Folder Structure
| Folder | Description |
|--------|----------------------------------------------------------------------------------------------------|
| /src | Root folder inside your module |
| /src/Magewire | All Magewire components live here (subfolder allowed) |
| /src/view/frontend/templates/magewire | All Magewire component template files live here (subfolder allowed) |
## JavaScript
The ```Magewire``` (alias for ```Livewire```) object is globally available on the page. More information can be found
inside the [Livewire docs](https://laravel-livewire.com/docs/2.x/reference#global-livewire-js).
### Document Events
- magewire:load
- magewire:update
- magewire:available
- magewire:loader:start ```(event) => {}```
- magewire:loader:stop ```(event) => {}```
### Lifecycle Hooks
[Read all about hooks](https://laravel-livewire.com/docs/2.x/reference#js-hooks)
## Block Structure
```xml
\My\Module\Magewire\Explanation\My\Module\Magewire\Explanation\My\Module\Magewire\Explanation
```
> **Note**: Template will automatically be set if a Magewire component has been set. When your component is named Foo,
> just create a foo.phtml inside the /view/templates/magewire folder.
## Dynamic Components
There are cases where you want to use the full power of Magewire, but the block is not configured in a layout XML.
### Blocks (dc)
```php
$this->layout->createBlock(Template::class)->toHtml()
```
To achieve this you have to add the same configuration as you do in a layout XML.
```php
use Magento\Framework\View\Element\Template;
use \Magento\Framework\App\ObjectManager;
$this->layout->createBlock(Template::class)
// Setting a template is optional since Magewire can auto-bind the belonging template.
->setTemplate('My_Module::path/to/component/phtml.phtml')
// Bind a valid Magewire component onto the block so it can be recognized by the layout.
->setData('magewire', ObjectManager::getInstance()->create(\My\Module\Magewire\Explanation::class))
->toHtml()
```
> **Note**: It's important to use create, otherwise when you try to use this Magewire component multiple they will
> share the data.
### Widgets (dc)
For widgets, the principle is the same. But here Magento does the rendering. So you cannot use ```setData()``` to assign the
Magewire component. But we can set the component via the ```constructor``` method.
```php
class MyWidget extends Template implements BlockInterface
{
public function __construct(
Context $context,
array $data = []
) {
$data['magewire'] = ObjectManager::getInstance()->create(\My\Module\Magewire\Explanation::class);
parent::__construct($context, $data);
}
}
```
### How does it work under the hood?
On the initial page request all information needed are here and we have no problem.
But on subsequent request Magento doesn't find the layout information.
To solve this issue all information that are needed to create the block are transported in the fingerprint "dynamic_layout". The information contains the magewire component and all block data (widget configuration).
With this information it is possible to dynamically create the missing block on a subsequent request.
## Templates
Options within your block template. The ```$magewire``` variable is by default available in your Component template.
```php
hasFoo();
// Get property value.
$magewire->getFoo();
// Call a custom method inside your component (if not set as uncallable).
$magewire->myCustomMethod();
// Overwrite a property after the (optional) mount method was executed.
$magewire->foo = 'barbar';
?>
```
### Switch Template
Switch to another template during a subsequent request.
```html
```
```php
public function login()
{
$this->switchTemplate('My_Module::customer/account/dashboard.phtml');
}
```
> **Tip**: Use the power of the layout xml to assign a "switch" template path as a data param assigned to the component.
> This way your component becomes more dynamic and extensible for other developers.
## Directives
### Wire Ignore
Ignore smart DOM diffing on specified elements within a Magewire component.
```html
= 'Foo: ' . $magewire->getFoo() ?>
```
#### Child Block Rendering
This can be very powerful when you are in the situation where you want to keep the child blocks intact.
```html
= 'Foo: ' . $magewire->getFoo() ?>
= $block->getChildHtml('child.block.name') ?>
```
### Wire Select
There are some specific cases where you have a select element including a ```wire:model``` attribute without any
modifiers. In these cases, you want to save a selected option on change. This works fine for typical mobile or mouse
interactions. But it has a couple of issues when using a keyboard where you have multiple options starting with the same
couple of letters.
The ```wire:select``` directive has two modifiers: ```debounce``` and ```blur```. By default, debounce uses a
```1500ms``` delay, but you can change this by specifying a different delay in milliseconds, such as
```debounce.2000ms```. blur syncs the model on element blur.
**Modifiers**
- Blur: Syncs model on element blur
- Debounce: Debounce on each keydown or select change
> **Important**: To use wire:select, you must always defer the model to let wire:select take over, and you cannot use a
> value with the directive.
Here's an example of how to use ```wire:select```:
```html