1
votes

I was wondering what the best practices when a creating a new record in a ember data application.

Witch following strategy is considered best practices?

Strategy 1

  • Model hook returns the promise from this.store.createRecord();
  • Each template fields is mapped to the model return by the model hook.
  • Action saves the model.

Strategy 2

  • Each template field is mapped to a controller property.
  • Action calls this.store.createdRecord() and save the model.
2
both strategies are perfectly valid and have their strengths and weaknesses. your strategy #2 however should probably not be directly bound controller properties. rather use a dummy/proxy object that can easily be cleaned up.Grapho

2 Answers

0
votes

I recently asked a similar question in the ember slack. The answer I got boiled down to: use Strategy #1, combined with ember-data-route addon.

From the addon's readme:

Ensure you clean up after your models.

Any routes you deactivate will check the model to ensure it is not unsaved. If it is it will either rollback or remove the model from the store depending if has been previously persisted.

0
votes

This depends, I use both strategies.

For very complex models with belongsTo and hasMany relationships, I usually use strategy #2 because most of the time it's simpler to clean some controller values than removing all models and depending relationships (when user cancels the action). I most of the time also use strategy 2 when editing a model with a hasMany, so the hasMany won't be altered directly.

For simple models, I would use strategy #1 and make sure to remove the model when the user does not save the changes (for example in the willTransition hook).

EDIT: in advance of @Grapho's comment, this ember addon might be handy:
https://github.com/yapplabs/ember-buffered-proxy