Ember-data: When to createRecord?

Imagine a form that saves a Thing:

this.store.createRecord('thing', myFormData);

During the period of time between the user pressing ‘Save’, and the server responding with a Failure, the UI looks as though it has actually succeeded, because a new Thing is in the store.

(It is then up to the developer to unload the record).

The desired behaviour is for the client side app not to know it has a new Thing until it actually does (i.e. once a save succeeds).

What are the available patterns for avoiding this?

Obviously you could manually POST and then push the new payload to the store. But most of the time a form is also used for editing a Thing. In which case it makes sense to use the same logic and just rely on .save()

Personally I use the records isSaving, isNew, isError booleans in combination with Array.filterBy / computed.filterBy.

@varblob Are you suggesting that creating the record client side (ahead of it actually existing on the server) is the the done thing in ember?. And then filtering all collections to exclude unsaved records so they don’t show in the UI?

I think that’s what I’m suggesting, assuming the “done thing means” that’s what ember-data does.

I think there is some kind of logic in that. Assuming that record is created as soon as request is sent to server means, that UI will seem super resposible to client. This approach used in StackOverflow and Facebook. Backend error is a rare thing so if you sent request to the client, you can assume it succeeded. And if backend returned error, you can easily notify user.

1 Like