Skip to content

Commit

Permalink
Debounce submission of the form
Browse files Browse the repository at this point in the history
  • Loading branch information
joegaudet committed Nov 3, 2022
1 parent e582d63 commit c9e1155
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion addon/components/form-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { A } from '@ember/array';
import { inject as service } from '@ember/service';
import { Promise } from 'rsvp';
import { tracked } from '@glimmer/tracking';
import { debounce } from '@ember/runloop';

export default class FormForComponent extends Component {
@service formFor;
Expand Down Expand Up @@ -798,7 +799,11 @@ export default class FormForComponent extends Component {
* @method doSubmit
* @public
*/
async doSubmit() {
doSubmit() {
return debounce(this, '_doSubmit', this.formFor.formActionDebounce ?? 0);
}

async _doSubmit() {
const lastDoSubmit = this.lastDoSubmit;

const model = this.model;
Expand Down Expand Up @@ -1191,6 +1196,7 @@ export default class FormForComponent extends Component {
updateValues(keyValues) {
return this.updateValuesFn(keyValues);
}

@action testClass(type) {
return `${type}-button`;
}
Expand Down
1 change: 1 addition & 0 deletions addon/services/form-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class FormForService extends Service {
this.customLabelComponent = this.config.customLabelComponent;
this.customInfoTextComponent = this.config.customInfoTextComponent;
this.preventsNavigationByDefault = this.config.preventsNavigationByDefault;
this.formActionDebounce = this.config.formActionDebounce;

this.router.on('routeWillChange', (transition) => {
if (
Expand Down
1 change: 1 addition & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function (/* environment, appConfig */) {

controlsFolder: 'form-controls',
controlPrefix: 'ff-',
formActionDebounce: 200,
},
},
};
Expand Down

0 comments on commit c9e1155

Please sign in to comment.