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 8411337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:24:07+00:00 2026-06-10T00:24:07+00:00

Creating a custom sync() method in backbone. I would like to do this the

  • 0

Creating a custom sync() method in backbone.

I would like to do this the “right” and interfere with Backbone’s normal functions as little as possible.

This is the code that I have so far:

var CustomSyncModel = Backbone.Model.extend({
    sync:function(method, model, options){
        var params = {
            type: 'POST'
            url: model.url(),
            error: function(jqXHR, textStatus, errorThrown){
                alert('error');
            },
            success: function(data, textStatus, jqXHR){
                model.parse(data);
            }
        };
        // Got this from line 1359 in Backbone.js developement library
        //     version 0.9.2:
        $.ajax(_.extend(params, options));
    }
 });

The issue that I am having is that the line: $.ajax(_.extend(params, options)); seems to be overwriting the custom success and error functions that I created. But I’m also concerned about interfering with any custom callbacks or other functionality that may have been specified elsewhere in the application that is using this model.

What is the “correct” way to go about overriding the Backbone’s sync() method?

Thanks!

  • 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-10T00:24:08+00:00Added an answer on June 10, 2026 at 12:24 am

    If you look at Model#fetch you’ll see the usual approach that Backbone uses:

    fetch: function(options) {
      //...
      var success = options.success;
      options.success = function(resp, status, xhr) {
        if (!model.set(model.parse(resp, xhr), options)) return false;
        if (success) success(model, resp);
      };
      //...
    }
    

    So Backbone just replaces the function with a new one that calls the original. In your case, you’d have something like this:

    // We don't own options so we shouldn't modify it,
    // but we can do whatever we want to a clone.
    options = _(options).clone()
    
    // Replace options.error with a wrapper.
    var error = options.error;
    options.error = function(jqXHR, textStatus, errorThrown) {
        alert('error');
        if(error)
            error(jqXHR, textStatus, errorThrown);
    };
    
    // Replace options.success with a wrapper.
    var success = options.success;
    options.success = function(data, textStatus, jqXHR) {
        model.parse(data);
        if(success)
            success(data, textStatus, jqXHR);
    };
    
    // We don't need error or success in here anymore.
    var params = {
        type: 'POST',
        url:   model.url() 
    };
    $.ajax(_.extend(params, options));
    

    BTW, your model.parse(data); in your success handler probably doesn’t do anything useful, parse should just be a simple filter so you’d want to do something (such as a model.set call) with the model.parse(data) return value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking into creating custom keyboard for Android, but I would like to
I'm creating custom table view cells, but result is incorrect. My code: - (UITableViewCell
I am reading up on creating custom jQuery plugins and am a little confused
I need to implement creating custom gesture(like Gesture Builder in Android ) in my
I just started with creating custom objects in javascript. I want to set this.someObjVar
I am creating custom code snippet templates using DevExpress' CodeRush. Is there anyway for
When creating custom typedefs for integers, is it possible for compiler to warn when
I've been following this guide on creating custom display attributes (specifically extra html attributes)
I am creating custom ViewGroup in which I am using onLayout method in which
Does anybody know of a method for creating custom Performance Counters using ordinary unmanaged

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.