How do backbonejs models handle server-side errors?

How do backbonejs models handle server-side errors?

When Im sending json with an array of errors, im still gets a successful callback with an array of errors inside the response parameter.

+6
source share
6 answers

Or maybe I should use http status. Currently, I have selected status 403 to indicate server-side validation errors. More status information can be found here: http://restpatterns.org/HTTP_Status_Codes

+3
source

In my client code, I check for the presence of an error attribute and react if necessary.

For example, I used the Collection.create function, which calls the add collection function if the request was successful. So, I redefined the Add Collection function to prevent the model from being added if it has the errors attribute and if it does not call the super method.

add: function(object, options) { if (_.isArray(object) || !object.get('errors')) { Backbone.Collection.prototype.add.call(this, object, options) } }, 

If my application returns a failure status code, it will work as well, as it will prevent successful callbacks from starting. But I did not like the idea of ​​returning the 400 error just because the submission was invalid. This is something that I have never had to do in applications other than Backbone.js. ("Invertebrates"?)

None of the descriptions for the 4XX status codes seem to fit the concept of a failed check. 400 and 403 come nearer, but nevertheless come off, as if they were intended for other purposes. On the other hand, the user really doesn’t care which status code you return; may also be 404.

It really is a question of whether you want to write more code on the server side or on the client side. My application more or less ignores the test result and returns a record serialized in JSON, regardless of whether it was saved or not, so I decided to work from this angle.

+7
source

Server-Side Trunk Verification:

Server side

Returns an array of errors

Client side

Pass "wait: true" as an option when calling model.save:

 modelVar.save(data,{ // here be options wait: true, success: this.processRequest, error: this.processErrors }); 

Update the model validation function to validate the error array:

 validate: function(attrs) { this.errors = []; if (attrs.errors && attrs.errors.length > 0) { for (var key in attrs.errors) { this.errors.push(attrs.errors[key].errorMessage); } } return _.any(this.errors) ? this.errors : null; } 

Result

The error callback will be launched and the model will not “change” if the server executes [errors].

UPDATE:

Starting with version 0.9.10 this will no longer work. You must either use an "invalid" event, or use the new options argument to the model.validate(attributes, options) function: if (typeof(options.error === 'function')) options.error();

+7
source

Backbone.js routes its ajax calls through any base library you have; JQuery or Zepto. Thus, basically, this library decides what constitutes success and what constitutes a mistake.

It looks like your server might return a 403 status code, but this is interpreted as a return to success. So you call your callback. For what it's worth, status code 403 seems strange to return errors if these errors are not related to authorization.

Is this what you were looking for?

0
source

I will talk using jQuery as your base js library (although I assume that zepto works the same way). Trunk routes async events like model.save () collection.fetch (), etc. Via Backbone.sync (). This function delegates $ .ajax () to make the actual ajax call. Thus, any error function that you specified when creating the object will be used by AS LONG. As a response header means an error, for example a 4XX header.

Often you see error handling in the success function because returning does not mean that the jquery error (when the response header is 2XX).

0
source

I usually return an HTTP message from the server along with a JSON error description, something like this:

 var xhr = myModel.save(); // On error show an alert xhr.fail(function () { try { // Assuming you are returning json error response like ["Error desc 01","Error desc 02"] errors = JSON.parse(xhr.responseText); alert(errors.join("\n")); } catch(e) { // Unknown error cause alert("The server failed to respond, please try again later."); } }); 
0
source

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


All Articles