Jump to content

Edit Backbone Ajax Success Method to Add in Custom Error Handling


unemployment

Recommended Posts

I have my own custom error code inside of a backbone ajax success method in case the server returns an error. The problem is that this code is repeated throughout my app and I wanted to edit the success function in one place so I don't have to constantly repeat this error handler in every ajax success. I want to edit the success function to include this error check wrapper. Do you know how to do that?

Here is an example of my success method in one of my views:

 

"success" : function success(model, data)
			    {
				 if(data['error'] !== undefined && data['error'].length === 0)
				  {
				   message('error', 'Whoops! System Error. Please refresh your page.');
				  }
			    else if(data['error'] !== undefined)
				 {
		          message('error', data['error']);
				 }
			    else
				 {
				  //add templates and do stuff here
				 }
			   }

 

Ideally I'd like to set that in a config somewhere and then I'd just be able to use:

 

"success" : function success(model, data)
	            {
				 // add templates and do stuff here
			    }

 

Is this possible? I tried using ajaxSetup but that didn't seem to work for me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.