You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Nathan Quirynen <na...@pensionarchitects.be> on 2018/12/21 14:28:40 UTC

Add javascript to every request response

Hi,

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

Nathan


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Add javascript to every request response

Posted by Nathan Quirynen <na...@pensionarchitects.be>.
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 <na...@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-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Add javascript to every request response

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
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 <ce...@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 <
> nathan@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-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>


-- 
Thiago

Re: Add javascript to every request response

Posted by Cezary Biernacki <ce...@gmail.com>.
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 <na...@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-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>