hypejunction / elgg-ajax-form
使用承诺处理表单提交
1.0.0
2019-08-01 11:06 UTC
Requires
- php: >=7.0
- composer/installers: ~1.0
- npm-asset/codemirror: ^5.43
This package is auto-updated.
Last update: 2024-08-29 05:36:13 UTC
README
使用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); });