Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Tapestry 5 Mixin for a loading spinner with spin.js
package com.yourapp.mixins;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
//Put the spin.min.js wherever on the context and point to it here
@Import(
library = {
"context:layout/assets/js/spin.min.js"
})
public class LoadingSpinner {
@Parameter(defaultPrefix = BindingConstants.LITERAL)
private boolean closeSpinner;
@Inject
private JavaScriptSupport javaScriptSupport;
@InjectContainer
private ClientElement clientElement;
public void afterRender() {
if ( closeSpinner) {
javaScriptSupport.addScript("$('.loader-wrapper').remove();");
} else {
javaScriptSupport.addScript(
"$('#%s').click(function(e) { "
+ "new Spinner().spin($('<div />', { class: 'loader-wrapper'}).appendTo('body')[0]); "
+ "});", clientElement.getClientId());
}
}
}
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
<!--
This example shows using the spinner mixin with a Tapestry-jQuery ajax dialog.
But it is just as easy to use in any other situation when a zone is rendered
(i.e. any actionlink, eventlink, form submit, etc. that are bound to a zone).
-->
<!-- when this is clicked, a spinner object will be added to the DOM -->
<t:jquery.dialogajaxlink dialog="myDialog" t:id="myDialogLink" class="btn btn-info" t:mixins="LoadingSpinner" zone="myDialogZone">
Long operation
</t:jquery.dialogajaxlink>
<t:jquery.dialog t:clientId="myDialog" title="My Dialog">
<t:zone t:id="mtDialogZone">
<!-- when this zone renders it will kill the spinner object -->
<t:any t:mixins="LoadingSpinner" t:closeSpinner="true" />
Wow! This took a very long time... lucky we had that spinner to make time go by faster.
</t:zone>
</t:jquery.dialog>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.