hypejunction/elgg-ajax-form

使用承诺处理表单提交

安装: 13

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

语言:JavaScript

类型:elgg-plugin

1.0.0 2019-08-01 11:06 UTC

This package is auto-updated.

Last update: 2024-08-29 05:36:13 UTC


README

Elgg 2.3

使用jQuery处理表单提交非常痛苦,尤其是当你需要绑定多个相互依赖的处理程序时。此插件提供了一个可以用来排队基于承诺的处理程序的模块。

var Form = require('ajax/Form');
var form = new Form('.my-form');
var Ajax = require('elgg/Ajax');

form.onSubmit(function(resolve, reject) {
	// execute a long running script, e.g. validate fields via ajax
	var ajax = new Ajax();
	ajax.post('somewhere').done(resolve).fail(reject);
})

form.onSubmit(function(resolve, reject) {
	console.log('hello');
	resolve();
});


// By default, once all promises are resolved, the form will be submitted via ajax,
// and the user will be forwarded to the URL specified by the response
// You can however register custom success callbacks to prevent redirection

form.onSuccess(function(data) {
	console.log(data);
	
	require('elgg/lightbox', function(lightbox) {
		lightbox.close();
	});
	
	$('.my-list').refresh();
});

// You can also add your own error handler

form.onError(function(error) {
	console.log(error);
});