6
votes

I have a simple ember-data model:

WZ.Exercise = DS.Model.extend
  name: DS.attr 'string'
  description: DS.attr 'string'
  group: DS.belongsTo 'WZ.Group'

I want to display a confirmation message to the user if a new record has been saved or if an error has occurred. The error could be that the the object is invalid and an error json is returned like below:

{"errors":{"description":["can't be blank"]}}

I can see that each model comes with an isSaving, isValid property and an isError property.

Can anybody tell me how I can use these properties to display the correct notifications to the users?

2
perhaps it would help: grosser.it/2012/05/05/… Otherwise, all I can say you is there is a discussion about validation with ember-data: github.com/emberjs/data/pull/201 but I don't know the development state of this, as currently Tom Dale and Yehuda Katz are working on a big refactoring. - sly7_7

2 Answers

3
votes

I can't help you with the validations part, but if you want to display information to the user based on the state of the data you can use these status in your view template like so:

{{#if content.isNew }}
  <button {{ action save }} >Save</button>
{{/if}}
{{#if content.isSaving }}
  <i>Saving record...</i>
{{/if }}
{{#if content.isLoaded }}
  <b>Record created</b>
{{/if }}
{{#unless content.isValid }}
  <error>Error saving data</error>
{{/unless }}
1
votes

Additional to sly7_7's first link (adding the ObserverSaveOnce function to DS.Model), you could patch the RESTadapter to catch the server-side error messages.

An example implementation you can find here: https://gist.github.com/3981832

(I did not paste the code here because I may update the gist for newer versions of ember-data)