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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:55:05+00:00 2026-06-02T20:55:05+00:00

I want to copy all attributes from backbone.js Model object to another if they

  • 0

I want to copy all attributes from backbone.js Model object to another if they are not undefined. It’s a little like a child overriding the attributes of a parent. There will be some attributes of the child object which should be excluded from the entire process. This is what I have so far:

function inherit(parent, child) {
    var childAttributes = Object.keys(child.attributes);

    // Don't involve these attributes in any way, they're special.
    attributes = _.without(attributes, ['specialCase1', 'specialCase2', 'specialCase3']);

    // This array will store undefined child attributes
    var undefChildAttributes = new Array();

   for (i in attributes) {
          var attr = attributes[i];           // Get attribute name
          var value = child.get(attr);        // ... and value
          var type = RealTypeOf(value);       // ... and type

          if (type == 'undefined') {
                 undefChildAttributes.push(attr);
          }       
   }

   // Update the child attributes array to not include undefined any attributes
   attributes = _.without(attributes, undefChildAttributes);

   // Copy any attributes from child to parent

   // ------------------ PROBLEM AREA ------------------
   for (var m=0; m<attributes.length; m++) {
          var attr = attributes[m];                // Attribute to copy

          // I want to use _.extends in some way here, maybe like:
          _.extends(parent, child[attr]);
   }
}

I’m stuck at the point of how to copy the attribute into the parent. The only way I have atm is a hack:

if (type == 'string') {
    eval('var o = {' + attr + ':"' + child.get(attr) + '"};');
 } else {
    eval('var o = {' + attr + ':' + child.get(attr) + '};');
}

parent.set(o);
  • 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-02T20:55:08+00:00Added an answer on June 2, 2026 at 8:55 pm

    You can avoid eval by using the bracket notation:

    var o = {};
    o[attr] = child.get(attr);
    parent.set(o);
    

    And my take on your code, if I got it right

    function inherit(parent, child) {
        var attributes = child.toJSON(),
            keepkeys = _.keys(attributes);
    
        // remove special cases
        keepkeys = _.without(keepkeys, 'specialCase1', 'specialCase2');
        // or keepkeys = _.difference(keepkeys, ['specialCase1', 'specialCase2']);
    
        // remove undefined values
        keepkeys = _.filter(keepkeys, function (key) {
            return typeof(attributes[key])!=="undefined";
        });
    
        // with underscore>=1.3.3
        var result = _.pick(attributes, keepkeys);
    
        // with underscore<1.3.3
        // var result = {};
        // _.each(keepkeys, function (key) {
        //    result[key] = attributes[key];
        // });
    
        parent.set(result);
    }
    
    var P = new Backbone.Model();
    var C = new Backbone.Model({
        copy1: "c1", 
        specialCase1: "do not copy", 
        undefkey: undefined
    });
    
    inherit(P, C);
    console.log(P.toJSON());
    

    And a Fiddle http://jsfiddle.net/tdCEF/

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

Sidebar

Related Questions

I want to copy values from one object to another object. Something similar to
i want to copy all files and folders from one drive to another drive
I want to copy all data from one database to another which has the
i want to copy a directory from one drive to another drive. My selected
I need to copy from input document to output document all attributes but one.
I want to copy all the *.jar files to another directory i wrote the
I want to copy all the rows from my table with single column update.
I want to copy a record with all his relations. I'm trying with: $o
I want to copy some functions from OpenCV library to my embedded application. Rewriting
I want to copy files that have timestamps from the time the script begins

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.