For example, a user keeps hitting a key when inside a textbox, and a save request is sent to the server to save the value.
Postponing consequent events by debouncing doesn't work because each keyUp is run in a new Run loop, unlike holding the key down a long time and entering characters that way.
So the question is: what's the most Ember Way approach to A) wait for a request to complete before sending a new one, to ensure an older value doesn't overwrite a newer value, and B) ensure that at least the very last request is sent, saving the most current user-entered value.
UPDATE:
After poking around for a bit, seems like a simple debounce is the only way I can accomplish what I wanted to:
keyUp: function(e) {
Ember.run.debounce(this, this.saveFieldValue, 2000);
},
saveFieldValue: function() {
const field = this.get('field');
field.save();
},
One important thing that I missed initially, which caused my saves to run each time a key was pressed, as opposed to once per debounce period, was that the function passed into the debounce method has to be named. Factoring out the save code and passing the reference to the function did the trick of saving a maximum of one times in a 2 second interval.
If you have a better "auto-save" approach, please post your answer here.
Aucun commentaire:
Enregistrer un commentaire