protected function parseRequest(WebRequest $request)
    {
        if ($this->model->isRootLeaf) {
            $eventState = $request->post("_leafEventState");

            if ($eventState !== null) {
                $eventState = json_decode($eventState, true);

                if ($eventState) {
                    $this->model->restoreFromState($eventState);
                }
            }
        }

        $targetWithoutIndexes = preg_replace('/\([^)]+\)/', "", $request->post("_leafEventTarget"));

        if ($targetWithoutIndexes == $this->model->leafPath) {
            $requestTargetParts = explode("_", $request->post("_leafEventTarget"));
            $pathParts = explode("_", $this->model->leafPath);

            if (preg_match('/\(([^)]+)\)/', $requestTargetParts[count($pathParts) - 1], $match)) {
                $this->model->leafIndex = $match[1];
                $this->model->updatePath();
            }

            $eventName = $request->post("_leafEventName");
            $eventTarget = $request->post("_leafEventTarget");

            $eventArguments = [];

            if ($request->post("_leafEventArguments")) {
                $args = $request->post("_leafEventArguments");
                foreach ($args as $argument) {
                    $eventArguments[] = json_decode($argument, $this->objectsToAssocArrays);
                }
            }

            if ($request->post("_leafEventArgumentsJson")) {
                $jsonArguments = json_decode($request->post("_leafEventArgumentsJson"), true);
                if (count($jsonArguments)) {
                    array_push($eventArguments, ...$jsonArguments);
                }
            }

            // Provide a callback for the event processing.
            $eventArguments[] = function ($response) use ($eventName, $eventTarget) {
                if ($response === null) {
                    return;
                }

                $type = "";

                if (is_object($response) || is_array($response)) {
                    $response = json_encode($response);
                    $type = ' type="json"';
                }

                print '<eventresponse event="' . $eventName . '" sender="' . $eventTarget . '"' . $type . '>
<![CDATA[' . $response . ']]>
</eventresponse>';
            };

            // First raise the event on the presenter itself
            $this->runBeforeRender(function () use ($eventName, $eventArguments) {
                $eventProperty = $eventName . "Event";

                if (property_exists($this->model, $eventProperty)) {
                    /** @var Event $event */
                    $event = $this->model->$eventProperty;
                    return $event->raise(...$eventArguments);
                }

                return null;
            });
        }
    }