Handles server-side user errors in ember-data when saving a model

Is there a way to handle a custom error while saving a model? To give an example, let's say I have a model with two properties, name and value. And when I do this:

var myModel = this.get('store').createRecord('myModel', {"name": "someName", "value": "someValue"}); myModel.save().then(function() { //if success //server responded with {"myModel:{"id":1,"name":"someName","value":"someValue"}"} },function() { //if failure //server responded with {"error":"some custom error message"} //BUT HOW TO CATCH THIS AND POSSIBLY REMOVE THE MODEL FROM THE STORE }); 

One way around this is to make an additional ajax call to check if the name is unique and then save. I'm just wondering what is the best / elegant approach here.

Thanks Dee

EDIT: I thought this might help give a little more context on the server side of things in groovy. So here it is:

In my controller, I:

 def create() { try { newRow = someService.create(params) render someService.list(newRow) as JSON//returns data in format needed by ember-data } catch (ValidationException ex) { def errors = ["errors":[]] ex.errors.allErrors.each{ if(it.arguments[0] == "fieldName" && it.code=="constrantViolated"){ errors.errors.push(["field":it.arguments[0],"message":"some custom message"]) } } //I am using 422 here because of post in http://stackoverflow.com/questions/7996569/can-we-create-custom-http-status-codes render(status: 422, contentType: 'JSON', text: (errors as JSON)) } } 

Then in my ember controller:

  var myModel = self.get('store').createRecord('myModel ', myModelDataInJSON); myModel .save().then(function () { //if success }, function (response) { myModel .deleteRecord(); var errors = $.parseJSON(response.responseText); for (var key in errors.errors) { //do something } }); 
+6
source share
3 answers

deleteRecord will delete the entry.

 myModel.save().then(function(response) { //if success //server responded with {"myModel:{"id":1,"name":"someName","value":"someValue"}"} },function(response) { //if failure //server responded with {"error":"some custom error message"} //BUT HOW TO CATCH THIS AND POSSIBLY REMOVE THE MODEL FROM THE STORE if(response.error=='no good'){ myModel.deleteRecord(); } }); 
+7
source

You can handle errors in the model by adding properties to your model:

 becameError: -> # handle error case here alert 'there was an error!' becameInvalid: (errors) -> # record was invalid alert "Record was invalid because: #{errors}" 

Check: How should errors be handled when using the RESTAdapter data of Ember.js?

+2
source

Why not answer a simple question: DS.ERRORS CLASS?

From the EmberJS docs:

For example, if you had a User model that looked like this:

App.User = DS.Model.extend({ username: attr('string'), email: attr('string') });

And you tried to save a record that was not checked on the server.

 var user = store.createRecord('user', { username: 'tomster', email: 'invalidEmail' }); user.save(); 

Your database data warehouse might return a response that looks like this. This answer will be used to populate the error object.

 { "errors": { "username": ["This username is already taken!"], "email": ["Doesn't look like a valid email."] } } 

Errors can be displayed to the user by accessing their property name or by using the messages property to get an array of all errors.

 {{#each errors.messages}} <div class="error"> {{message}} </div> {{/each}} 

Is this question considered only in model validation? compared to saving / saving, so the data is already clean before it gets to the data store ... It looks like you still want to manage errors at the adapter data store level.

That's all said, why don't you just use template or standard JS checking at the user interface level?

0
source

Source: https://habr.com/ru/post/956668/


All Articles