title: Edit Backbone Ajax Success Method to Add in Custom Error Handling
body: <p>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?</p>
  
  <p>Here is an example of my success method in one of my views:</p>
  
  <pre><code>"success" : function success(model, data)
               {
                if(data['error'] !== undefined &amp;&amp; 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
                 }
               },
  </code></pre>
  
  <p>Ideally I'd like to set that in a config somewhere and then I'd just be able to use:</p>
  
  <pre><code>"success" : function success(model, data)
               {
                // add templates and do stuff here
               }
  </code></pre>
  
  <p>Is this possible? I tried using ajaxSetup but that didn't seem to work for me.</p>
  
  <p>UPDATED CODE STILL NOT WORKING:</p>
  
  <p>That does get me a little further along but the error handler isn't functioning as a wrapper.  The data is not being passed into my ajax calls.  In fact, my success methods on my ajax calls aren't running at all anymore.  I tried console.log("some text") in my ajax calls but nothing is being output.  Do you know what is wrong with this?</p>
  
  <pre><code>// Save the original Sync method
  defaultSync = Backbone.sync;
  
  //Over ride Backbone async
  Backbone.sync = function(method, 
                           model, 
                           options) 
                   {
                    success = options.success
  
                    options.success = function(data) 
                                       {
                                        if(data['error'] !== undefined &amp;&amp; data['error'].length === 0)
                                         {
                                          message('error', 'Whoops! System Error. Please refresh your page.');
                                         }
                                        else if(data['error'] !== undefined)
                                         {
                                          message('error', data['error']);
                                         }
                                        else
                                         {
                                          success(model, 
                                                  data);
                                         }
                                       }
  
                    return defaultSync(method, 
                                       model, 
                                       options)
                   }
  </code></pre>