Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:45:07+00:00 2026-06-15T05:45:07+00:00

Background: Backbone model provides an option to register a fallback error handler that will

  • 0

Background:

Backbone model provides an option to register a fallback error handler that will be called each time a call to the server fails and no specific handler is provided.

MyModel = new Backbone.Model.extend({
  initialize: function(option) {
    this.on("error", this.fallbackErrorHandler);
  },

  fallbackErrorHandler: function() {
    // default behavior in case of an error
  },

  foo: function() {
    this.fetch();             // fallbackErrorHandler will be called
    this.fetch({              // the inline error handler will be called
      error: function() {
        // some specific code for this error
      }
    });
  }    
});

What I want to do:

I would like all the backbone models in my application to have a fallback error handler.
But – I don’t want to explicitly add the handler for each model.
I would like to define and register the handler once and have it applied to each backbone model in my application.
In addition, I don’t want to change existing code and I don’t want to change the way developers define Models in my application.

What I already tried:

I added an initializer which registers a handler on the Backbone.Model.prototype object.

App.Marionette.addInitializer(function() {
  Backbone.Model.prototype.on("error",function(){
     // default behavior in case of an error
  });
});

This code worked and my fallback error handler was called.
However, the side affect of this code was that all error handlers registered in any backbone models in the application were registered on the Backbone.Model.prototype instead of on the Backbone.Model. So obviously this is not a good solution.

I also thought about defining MyModel which extends the Backbone.Model and registers the default handler. All other models in my application will then extend MyModel instead of directly extending Backbone.Model.
However, I’m trying to make this seamlessly without having to change existing code and then instructing all the developer to extend MyModel instead of Backbone.Model.

Does anyone have a solution for this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:45:08+00:00Added an answer on June 15, 2026 at 5:45 am

    Backbone uses jQuery’s AJAX method calls by default. You can hook in to this directly, instead of using Backbone:

    http://api.jquery.com/ajaxError/

    If that doesn’t give you what you want, you can override the “fetch” method in Backbone’s model:

    
    var originalFetch = Backbone.Model.prototype.fetch;
    
    Backbone.Model.prototype.fetch = function(options){
    
      var originalError = options.error;
    
      options.error = function(model, error){
        if (originalError){ originalError(model, error); }
    
        // call your global error handler here.
        App.myGlobalErrorHandler(model, error);
    
      }
    
      originalFetch.apply(this, arguments);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a quick sample Backbone.js app that has a Post model,
Background I'm working on a an educational JavaScript application/site (SPA) that will eventually have
Background I have been using Backbone.js for some time and one aspect of it
I'm building a Backbone.js app that will be fairly complex. Coming from a Rails
Background: We're building an application that allows our customers to supply data in a
Background: I would like to dismiss a modalView that I have presented earlier and
My background is jQuery/require.js/backbone.js! In order to broaden my horizon I'm taking a closer
Background: My employeer at my non-programming job knows that I am an undergraduate CS
Background: At my company we are developing a bunch applications that are using the
Background: Some time ago, I built a system for recording and categorizing application crashes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.