That's a nice solution, Cezary!

Another one is to create a mixin and apply it to all component pages by
implementing a ComponentClassTransformer and contributing it to the
ComponentClassTransformWorker and using
MutableComponentModel.addMixinClassName().

On Fri, Dec 21, 2018 at 6:25 PM Cezary Biernacki <cezary...@gmail.com>
wrote:

> Yes, it possible. There are probably several ways to do that, one of them
> is by contributing to MarkupRenderer and PartialMarkupRenderer. Start first
> by putting your JavaScript in a JavaScript module, META-INF/modules
> directory define a JavaScript file like this (of course customise it to
> whatever you need):
>
> META-INF/modules/mymodule.js
>
> define([], function () {
>         var exports = {};
>         exports.greetings = function (name) {
>             console.log("Hello " + name);
>         };
>
>         return exports;
>     }
> );
>
>
> Then define a Tapestry service that invokes this JavaScript code using
> Tapestry's JavaScriptSupport service:
>
> GreetingsInserter.java (I skipped all usual imports and package definitions
> for brevity here)
>
> public class GreetingsInserter {
>     private final JavaScriptSupport javaScriptSupport;
>
>     public GreetingsInserter(final JavaScriptSupport javaScriptSupport) {
>         this.javaScriptSupport = javaScriptSupport;
>     }
>
>     public void insertGreetings() {
>
> javaScriptSupport.require("mymodule").invoke("greetings").with("World");
>     }
> }
>
>
> now in Tapestry's module class (e.g. AppModule.jave) you need to bind this
> service
>
> public static void bind(@Nonnull final ServiceBinder binder) {
>     // ... usually there are bindings for other services here too
>    binder.bind(GreetingsInserter.class);}
>
>
> and add contributions, like this:
>
> @Contribute(MarkupRenderer.class)
> public static void
>
> addExampleGreetingsMarkupRendererContribution(OrderedConfiguration<MarkupRendererFilter>
> configuration, GreetingsInserter greetingsInserter) {
>     configuration.add("greetings", (writer, renderer) -> {
>         greetingsInserter.insertGreetings();
>         renderer.renderMarkup(writer);
>     }, "after:JavaScriptSupport");
> }
>
> @Contribute(PartialMarkupRenderer.class)
> public static void
>
> addExampleGreetingsContribution(OrderedConfiguration<PartialMarkupRendererFilter>
> configuration, GreetingsInserter greetingsInserter) {
>     configuration.add("greetings", (writer, reply, renderer) -> {
>         greetingsInserter.insertGreetings();
>         renderer.renderMarkup(writer, reply);
>     }, "after:JavaScriptSupport");
> }
>
>
> It should now work for both normal and AJAX calls. You can potentially
> invoke different JavaScript codes depending on requests, though I would
> recommend avoiding too complex logic as any error might totally disable
> your application including even error pages. You can play with
> initialisation priorities to control when this JavaScript code is invoked
> during rendering, e.g.:
>
> public void insertGreetings() {
>
> javaScriptSupport.require("mymodule").invoke("greetings").priority(InitializationPriority.EARLY).with("World");
> }
>
>
> Best regards,
> Cezary
>
>
> On Fri, Dec 21, 2018 at 3:39 PM Nathan Quirynen <
> nat...@pensionarchitects.be>
> wrote:
>
> > Hi,
> >
> > Is it possible to add javascript or some javascript module call to every
> > (also xhr) requests response?
> >
> > Nathan
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> > For additional commands, e-mail: users-h...@tapestry.apache.org
> >
> >
>


-- 
Thiago

Reply via email to