Re: Add javascript to every request response
Nathan Quirynen <[email protected]> Mon, 24 Dec 2018 17:20:22 +0100
| Newsgroups | gmane.comp.java.tapestry.user |
|---|---|
| Organization | Pension Architects |
| Message-ID | <[email protected]> |
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 <[email protected]>
> wrote:
>
>> Hi,
>>
>> Is it possible to add javascript or some javascript module call to every
>> (also xhr) requests response?
>>
>> Nathan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]