You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?phpnamespaceAppBundle\Fragment;
useMassive\Component\Web\ComponentTwigExtension;
useSymfony\Component\HttpFoundation\Request;
useSymfony\Component\HttpFoundation\Response;
useSymfony\Component\HttpKernel\Controller\ControllerReference;
useSymfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer;
useSymfony\Component\HttpKernel\UriSigner;
/** * Register and render the div for ajax-include component. */class JsFragmentRenderer extends RoutableFragmentRenderer
{
/** * @var ComponentTwigExtension */private$webComponentTwigExtension;
/** * @var UriSigner */private$signer;
/** * JsFragmentRenderer constructor. * * @param ComponentTwigExtension $webComponentTwigExtension * @param UriSigner $signer */publicfunction__construct(
ComponentTwigExtension$webComponentTwigExtension,
UriSigner$signer
) {
$this->webComponentTwigExtension = $webComponentTwigExtension;
$this->signer = $signer;
}
/** * {@inheritdoc} */publicfunctionrender($uri, Request$request, array$options = [])
{
if ($uriinstanceof ControllerReference) {
if (null === $this->signer) {
thrownew \LogicException(
'You must use a proper URI when using the JS rendering strategy or set a URL signer.'
);
}
$fragmentUri = $this->generateFragmentUri($uri, $request, true);
if (!parse_url($fragmentUri)) {
// FIXME sulu-preview can not generate a property URL. This if can be removed when it is fixed there.returnnewResponse('');
}
// we need to sign the absolute URI, but want to return the path only.$uri = substr($this->signer->sign($fragmentUri), strlen($request->getSchemeAndHttpHost()));
}
$id = $this->webComponentTwigExtension->registerComponent('ajax-include', ['uri' => $uri]);
returnnewResponse(
sprintf('<div id="%s" data-src="%s"></div>', $id, $uri)
);
}
/** * {@inheritdoc} */publicfunctiongetName()
{
return'js';
}
}
importajaxCachefrom'./../services/ajax-cache';/** * @param {String} [options.uri] Define the uri which is requested. */exportdefault{uri: null,/** * @method initialize */initialize: function($el,options){this.$el=$el;this.options=options;this.uri=this.options.uri;this.refresh();},refresh: function(){ajaxCache.load(this.uri).done(function(data){this.updateContent(data);}.bind(this));},updateContent: function(content){this.$el.html(content);}};
While the JS part make sense and we should have a deep look into the component @martinlagler created in Kuegö project as it supports also lazy loading. The twig part is not required as we can use the fragment_uri twig extension instead e.g.:
Sometimes you want to replace a
render_esi
with a ajax request.The text was updated successfully, but these errors were encountered: