Saving while typing

I’ve got a few text fields in our app where we save as the user types. I’ve used ember-concurrency, similar to this article. A problem I’m running into is if the the user pauses typing, the continues while the data has been sent to the API, but is still waiting on a response. The user might type a word, which is immediately removed when the persisted data comes back and ember-data inserts it into the store.

What are some common patterns to make save-as-you-type work elegantly?

Sounds like you need to use restartable tasks in ember-concurrency:

You either have to “buffer” the changes (like changeset) between saves or not return those fields in the response.

I am using a restartable task, which calls .perform() on a keepLatest task. In theory, if multiple restartable tasks are called, only the one should make it through. Though…I’m not sure if keepLatest is necessary. The restartable task should suffice.

keepLatest would function as you describe in your question, so that’s not what you want. It works similarly to enqueue, where it doesn’t cancel a running task, except that it will drop all others except the latest while a task is running.

As a follow-up to this problem I was having, the answer to my problem was to ensure the defined tasks were available as properties on the controller. The template is dependent upon these to derive the state of the task. One can either define the tasks directly on the controller, or pass tasks defined on the route to the controller via the setupController hook on the route file.

1 Like