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

  • SEARCH
  • Home
  • 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 8354865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:41:15+00:00 2026-06-09T09:41:15+00:00

I am working in jquery with backbone.js and running into the case where i

  • 0

I am working in jquery with backbone.js and running into the case where i need to duplicate models, but i need to do a deep copy on them so no references exist between the copies. Models can have other models as attributes. Models can have anon functions as attributes.

So i’m working on creating an algorithm that will deep clone most backbone models. I’m expecting that all bindings should be removed (for the new instance) during this copy so I’m not worried about trying to keep them.

Goals:

  • Able to duplicate all simple variable (String, Int, float, etc) and store it into the new model, as the same name.
    • Done, using toJSON to create a new JSON object which can be passed to set(). This object only contains simple attributes, i.e. does not include attributes assigned to functions or other models.
  • Able to duplicate the anon functions some variables will be assigned to, without knowing function/attribute names beforehand.
    • If I know the name of the attribute which is assigned to a function I can copy it. But if the model is new or unknown I don’t have that information.
  • If an attribute is another backbone model, call the deep copy algorithm recursively on that attribute.
    • Not able to check if an attribute is a backbone model with native backbone methods, looking for a work around.

The simplified version of what I currently have is below:

/**
 * Performs a deep copy of a backbone.js model
 * All bindings for the copy are lost
 * @param orgModel - the original model to copy
 */
function deepCopyModel(orgModel)
{   
    var dupModel = Backbone.Model.extend({});

    var orgAttributes= orgModel.toJSON();

    var keepAttr=_.keys(orgAttributes);
    //remove any special cases
    keepAttr=_.without( keepAttr , 'specialCase1', 'specialCase2' );
    //or keepAttr=_.difference(keepAttr, ['specialCase1', 'specialCase2'] );

    //remove undefined values
    keepAttr=_.filter(keepAttr,function(key) {
        return ( typeof(attributes[key])!="undefined" );
    });
    //grab the resulting list of attributes after filtering
    var result=_.pick(attributes,keepAttr);
    //assign attributes to the copy using set
    dupModel.set(result);

    //TODO: Implement deep copy of functions

    //TODO: Implement deep copy of inner models

        return dupModel;
}

Any help or insight you can give would be greatly appreciated. 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-09T09:41:17+00:00Added an answer on June 9, 2026 at 9:41 am

    jQuery’s extend method allows you to simply copy object properties from one to another.

    Here’s a contrived, but illustrative example. It even shows why you would not need to “deep” copy functions!

    var someObj = {
        a : "a",
        b : 12345,
        c : {
            d : "d",
            e : "e"
        },
        f : function() {
            alert(this.a);
        }
    };
    
    //copy from original to new empty object
    var deepCopy = $.extend(true, {}, someObj);
    
    deepCopy.a = "deepCopy.a";
    deepCopy.c.d = "deepCopy.c.d";
    
    alert("someObj is not affected when deep copying: " + someObj.c.d);
    alert("deepCopy is entirely distinct when deep copying: " + deepCopy.c.d);
    
    deepCopy.f();    
    someObj.f();
    

    Here’s a fiddle for your convenience: http://jsfiddle.net/S6p3F/3/

    Running this code you will see that someObj and deepCopy are identical in structure but distinct objects.

    As you can see, deep copying of functions is not required as the this reference is bound to whatever object the function is applied to. This is becausein javascript, calling a function as deepCopy.f() is functionally equivalent to deepCopy.f.call(deepCopy). A more illustrative example:

    function someFunction() {
        alert(this.someProperty);
    }
    
    var a = {
            someProperty: "a's property"
        },
        b = {
            someProperty: "b's property"
        };
    
    someFunction.call(a);
    someFunction.call(b);
    

    And a fiddle: http://jsfiddle.net/S6p3F/2/

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

Sidebar

Related Questions

I'm working with jQuery's Ajax but in my case I need to store the
I've been working on a prototype Backbone application using Backbone.LayoutManager and I'm running into
On IE I can do this with the (terribly non-standard, but working) jQuery if
I've got a drag and drop UI basically working with jquery ui and backbone.js.
My PHP code return JSON data to jquery autocomplete but autocomplete not working Jquery
I'm working with jQuery for the first time and need some help. I have
In an existing Backbone/jQuery/CoffeeScript app I am working on, it appears there is a
Why is the jquery selector not working? View Views.Now = Backbone.View.extend({ template:mytemplate, initialize:function ()
I have a working jQuery page redirect. I want to fade into the page
I'm quite a noob to Backbone.js, but I love jQuery. However I love how

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.