Skip to content

Commit fe35ec8

Browse files
committed
Events
1 parent d8ef32a commit fe35ec8

File tree

1 file changed

+106
-9
lines changed
  • resources/views/docs/mobile/2/the-basics

1 file changed

+106
-9
lines changed

resources/views/docs/mobile/2/the-basics/events.md

Lines changed: 106 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,72 @@ Dialog::toast('Hello!');
2424
```
2525
Asynchronous actions trigger operations that may complete later. These return immediately, usually with a `bool` or
2626
`void`, allowing PHP's execution to finish. In many of these cases, the user interacts directly with a native component.
27-
When the user has completed their task and the native UI is dismissed, the native app
27+
28+
When the user has completed their task and the native UI is dismissed, the app will emit an event that represents the
29+
outcome.
30+
31+
The _type_ (the class name) of the event and its properties all help you to choose the appropriate action to take in
32+
response to the outcome.
2833

2934
```php
3035
// These trigger operations and fire events when complete
3136
Camera::getPhoto(); // → PhotoTaken event
32-
Biometrics::promptForBiometricID(); // → Completed event
33-
PushNotifications::enrollForPushNotifications(); // → TokenGenerated event
37+
Biometrics::prompt(); // → Completed event
38+
PushNotifications::enroll(); // → TokenGenerated event
3439
```
3540

3641
## Basic Event Structure
3742

3843
All events are standard [Laravel Event classes](https://laravel.com/docs/12.x/events#defining-events). The public
3944
properties of the events contain the pertinent data coming from the native app side.
4045

46+
## Custom Events
47+
48+
Almost every function that emits events can be customized to emit events that you define. This is a great way to ensure
49+
only the relevant listeners are executed when these events are fired.
50+
51+
Events are simple PHP classes that receive some parameters. You can extend existing events for convenience.
52+
53+
Let's see a complete example...
54+
55+
### Define your custom event class
56+
57+
```php
58+
namespace App\Events;
59+
60+
use Native\Mobile\Events\Alert\ButtonPressed;
61+
62+
class MyButtonPressedEvent extends ButtonPressed
63+
{}
64+
```
65+
66+
### Pass this class to an async function
67+
68+
```php
69+
use App\Events\MyButtonPressedEvent;
70+
71+
Dialog::alert('Warning!', 'You are about to delete everything! Are you sure?', [
72+
'Cancel',
73+
'Do it!'
74+
])
75+
->event(MyButtonPressedEvent::class)
76+
```
77+
78+
### Handle the event
79+
80+
Here's an example handling a custom event class inside a Livewire component.
81+
82+
```php
83+
use App\Events\MyButtonPressed;
84+
use Native\Mobile\Attributes\OnNative;
85+
86+
#[OnNative(MyButtonPressed::class)]
87+
public function buttonPressed()
88+
{
89+
// Do stuff
90+
}
91+
```
92+
4193
## Event Handling
4294

4395
All asynchronous methods follow the same pattern:
@@ -52,10 +104,18 @@ allows you to listen for these events in the context that best suits your applic
52104
### On the frontend
53105

54106
Events are 'broadcast' to the frontend of your application via the web view through a custom `Native` helper. You can
55-
easily listen for these events in JavaScript in two ways:
107+
easily listen for these events through JavaScript in a few ways:
108+
109+
- The globally available `Native.on()` helper
110+
- Directly importing the `on` function
111+
- The `#[OnNative()]` PHP attribute Livewire extension
112+
113+
<aside>
56114

57-
- The `Native.on()` helper
58-
- Livewire's `#[On()]` attribute
115+
Typically, you shouldn't need to use more than one of these approaches. Which one you adopt will depend on which
116+
frontend stack you're using to build your app.
117+
118+
</aside>
59119

60120
#### The `Native.on()` helper
61121

@@ -71,12 +131,49 @@ Register the event listener directly in JavaScript:
71131
</script>
72132
```
73133

74-
#### Livewire's `#[On()]` attribute
134+
This approach is useful if you're not using any particular frontend JavaScript framework.
135+
136+
#### The `on` import
137+
138+
If you're using a SPA framework like Vue or React, it's more convenient to import the `on` function directly to
139+
register your event listeners. Here's an example using the amazing Vue:
140+
141+
```js
142+
import { on, Events } from '#nativephp';
143+
import { onMounted } from 'vue';
144+
145+
const handleButtonPressed = (index, label) => {};
146+
147+
onMounted(() => {
148+
on(Events.Alert.ButtonPressed, handleButtonPressed);
149+
});
150+
```
151+
152+
Note how we're also using the `Events` object above to simplify our use of built-in event names. For custom event
153+
classes, you will need to reference these by their full name:
154+
155+
```js
156+
on('App\\Events\\MyButtonPressedEvent', handleButtonPressed);
157+
```
158+
159+
In SPA land, don't forget to de-register your event handlers using the `off` function too:
160+
161+
```js
162+
import { off, Events } from '#nativephp';
163+
import { onUnmounted } from 'vue';
164+
165+
onUnmounted(() => {
166+
off(Events.Alert.ButtonPressed, handleButtonPressed);
167+
});
168+
```
169+
170+
#### The `#[OnNative()]` attribute
75171

76-
Livewire makes listening to 'broadcast' events simple. Just add the event name, prefixed by `native:` to the `#[On()]`
77-
attribute attached to the method you want to use as its handler:
172+
Livewire makes listening to 'broadcast' events simple. Just add the `#[OnNative()]` attribute attached to the Livewire
173+
component method you want to use as its handler:
78174

79175
```php
176+
use Native\Mobile\Attributes\OnNative;
80177
use Native\Mobile\Events\Camera\PhotoTaken;
81178

82179
#[OnNative(PhotoTaken::class)]

0 commit comments

Comments
 (0)