Add javascript to every request response

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Add javascript to every request response

nquirynen
Hi,

Is it possible to add javascript or some javascript module call to every
(also xhr) requests response?

Nathan


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Add javascript to every request response

Cezary Biernacki
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 <[hidden email]>
wrote:

> Hi,
>
> Is it possible to add javascript or some javascript module call to every
> (also xhr) requests response?
>
> Nathan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Add javascript to every request response

Thiago H de Paula Figueiredo
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 <[hidden email]>
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 <
> [hidden email]>
> wrote:
>
> > Hi,
> >
> > Is it possible to add javascript or some javascript module call to every
> > (also xhr) requests response?
> >
> > Nathan
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>


--
Thiago
Reply | Threaded
Open this post in threaded view
|

Re: Add javascript to every request response

nquirynen
In reply to this post by Cezary Biernacki
Thanks a lot for the very detailed answer!

Exactly what I needed.

Op 21/12/18 om 21:05 schreef Cezary Biernacki:

> 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 <[hidden email]>
> wrote:
>
>> Hi,
>>
>> Is it possible to add javascript or some javascript module call to every
>> (also xhr) requests response?
>>
>> Nathan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]