Jump to content


Edit Backbone Ajax Success Method to Add in Custom Error Handling

  • Please log in to reply
1 reply to this topic

#1 unemployment

  • Members
  • PipPipPip
  • Advanced Member
  • 746 posts

Posted 24 February 2013 - 08:20 PM

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']);
					  //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.

#2 Jessica

  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 24 February 2013 - 09:03 PM

define the function, then your success function can either be a plain wrapper for it, or I think you can just send the string name of the function. 
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users