If you have used Facebook's React library, then you are familiar with the concept of application state. React components are, at their core (and as noted in the official documentation), simple state machines. This declarative approach to building a UI may take some adjusting to, but it ultimately simplifies kludgy imperative code into smaller, much more manageable pieces.
This pattern of manipulating state and responding to those changes can be implemented to great effect using the Symfony Event Dispatcher. This talk will step through this state-based approach to building an easily maintained and testable PHP application, tease out a few gotchas, and share real-world applications.
15. “React thinks of UIs as simple state machines. By
thinking of a UI as being in various states and rendering
those states, it's easy to keep your UI consistent.”
https://facebook.github.io/react/docs/interactivity-and-dynamic-uis.html#components-are-just-state-machines
19. $('#my_button').click(function() {
var $btn = $(this);
var clicks = $btn.text().match(/d+/)[0];
var text = clicks + ' click';
if (clicks != 1) text += 's';
$btn.text(text);
})
20. $('#my_button').click(function() {
var $btn = $(this);
var clicks = $btn.text().match(/d+/)[0];
var text = clicks + ' click';
if (clicks != 1) text += 's';
$btn.text(text);
})
oops…
28. Take the next right
• Depress the gas pedal and increase your speed to 10mph
• Engage your right turn signal
• As you approach the intersection, remove your foot from the gas
pedal and gently apply the brake
• Check for pedestrians crossing the intersection
• Check your mirrors and blind spot for bicyclists
• Lift your foot from the brake and slowly increase speed by
depressing the gas pedal
• Turn the steering wheel to the right
29. Take the next right
• Depress the gas pedal and increase your speed to 10mph
• Engage your right turn signal
• As you approach the intersection, remove your foot from the gas
pedal and gently apply the brake
• Check for pedestrians crossing the intersection
• Check your mirrors and blind spot for bicyclists
• Lift your foot from the brake and slowly increase speed by
depressing the gas pedal
• Turn the steering wheel to the right
declarative
30. Take the next right
• Depress the gas pedal and increase your speed to 10mph
• Engage your right turn signal
• As you approach the intersection, remove your foot from the gas
pedal and gently apply the brake
• Check for pedestrians crossing the intersection
• Check your mirrors and blind spot for bicyclists
• Lift your foot from the brake and slowly increase speed by
depressing the gas pedal
• Turn the steering wheel to the right
imperative
31. $('#my_button').click(function() {
var $btn = $(this);
var clicks = $btn.text().match(/d+/)[0];
var text = clicks + ' click';
if (clicks != 1) text += 's';
$btn.text(text);
})
44. The Event Dispatcher component provides
tools that allow your application components
to communicate with each other by
dispatching events and listening to them.
symfony.com
64. class UserManager
{
public function persistUser(User $user)
{
$this->em->persist($user);
$this->dispatcher->dispatch(
UserEvents::CREATE,
new UserEvent($user)
);
}
}