I'm currently exploring the most effective method to prevent adding a record when encountering an error using Ember Data:
Here's the code snippet in question:
createUser: function() {
// Create the new User model
var user = this.store.createRecord('user', {
firstName: this.get('firstName'),
lastName: this.get('lastName'),
email: this.get('email')
});
user.save().then(function() {
console.log("User saved.");
}, function(response) {
console.log("Error.");
});
},
I've implemented schema validation on the backend and it returns a 422 Error if validation fails.
If I ignore the error, the record gets added to the site and I also encounter a console error.
To address this issue, I made the following modification:
user.save().then(function() {
console.log("User saved.");
}, function(response) {
user.destroyRecord();
});
While this partially solves the problem by removing the record after handling the server response, two issues remain:
1) The record still briefly appears and disappears (like a visual glitch).
2) The console error persists.
Is there a more effective way to handle this situation? Is there a way to avoid adding the record when an error is returned from the server? Can the console error be suppressed?
Any insights or suggestions would be greatly appreciated. Thank you.