Skip to content

Instantly share code, notes, and snippets.

@picodotdev
Last active December 25, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save picodotdev/6938225 to your computer and use it in GitHub Desktop.
Save picodotdev/6938225 to your computer and use it in GitHub Desktop.
Solución al doble envío de peticiones en las aplicaciones web http://elblogdepicodev.blogspot.com.es/2013/10/solucion-al-doble-envio-de-peticiones.html
$ git clone git://github.com/picodotdev/elblogdepicodev.git
$ cd elblogdepicodev/PlugInTapestry
$ ./gradlew tomcatRun
# Abrir en el navegador http://localhost:8080/PlugInTapestry/
<div class="row">
<div class="col-md-4">
<h5><b>Con</b> mixin en <br/> form (ajax)</h5>
<form t:id="submitOneForm2" t:type="form" t:zone="submitOneZone" t:mixins="submitOne">
<input t:type="submit" value="Sumar 1"/>
</form>
</div>
<div class="col-md-4">
<h5><b>Con</b> mixin en <br/> botón (ajax)</h5>
<form t:id="submitOneForm3" t:type="form" t:zone="submitOneZone">
<input t:type="submit" value="Sumar 1" t:mixins="submitOne"/>
</form>
</div>
<div class="col-md-4">
<h5><b>Con</b> mixin en <br/> enlace (ajax)</h5>
<a t:type="eventlink" t:event="sumar1CuentaAjaxSubmitOne" t:zone="submitOneZone" t:mixins="submitOne">Sumar 1</a>
</div>
</div>
package es.com.blogspot.elblogdepicodev.plugintapestry.mixins;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
public class SubmitOne {
@Inject
private JavaScriptSupport support;
@InjectContainer
private ClientElement element;
@Inject
private ComponentResources resources;
public void afterRender() {
JSONObject spec = new JSONObject();
spec.put("elementId", element.getClientId());
support.require("app/submitOne").invoke("init").with(spec);
}
}
define("app/submitOne", ["jquery"], function($) {
var SubmitOne = function(spec) {
this.spec = spec;
this.timeout = null;
var element = $('#' + this.spec.elementId);
this.blocked = false;
var _this = this;
$(document).ajaxStart(function() {
_this.onAjaxStart();
});
$(document).ajaxStop(function() {
_this.onAjaxStop();
});
if (element.is('form')) {
element.on('submit', function(event) {
return _this.onSubmit(event);
});
} else {
element.on('click', function(event) {
return _this.onSubmit(event);
});
}
}
SubmitOne.prototype.onSubmit = function(event) {
if (this.isBlocked()) {
event.preventDefault();
return false;
} else {
this.block();
return true;
}
}
SubmitOne.prototype.onAjaxStart = function() {
this.block();
}
SubmitOne.prototype.onAjaxStop = function() {
this.unblock();
}
SubmitOne.prototype.isBlocked = function() {
return this.blocked;
}
SubmitOne.prototype.block = function() {
this.blocked = true;
}
SubmitOne.prototype.unblock = function() {
this.blocked = false;
}
function init(spec) {
new SubmitOne(spec);
}
return {
init: init
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment