Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember-data isValid, isSaving and isError

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?

like image 343
dagda1 Avatar asked Jul 24 '12 20:07

dagda1


2 Answers

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 }}
like image 76
buuda Avatar answered Nov 15 '22 12:11

buuda


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)

like image 21
Niklas Hofer Avatar answered Nov 15 '22 10:11

Niklas Hofer