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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:45:00+00:00 2026-06-09T22:45:00+00:00

I am trying to implement some sort of nested collections in a Backbone.Model To

  • 0

I am trying to implement some sort of nested collections in a Backbone.Model

To do this I have to overwrite the adapter functions which parse the server response and wrap the array into a collection and the function which serializes the whole object without any helper methods. I am having problems with the second one.

var Model = Backbone.Model.extend({

    urlRoot: "/model",

    idAttribute: "_id",

    // this wraps the arrays in the server response into a backbone  collection
    parse: function(resp, xhr) {
        $.each(resp, function(key, value) {
            if (_.isArray(value)) {
                resp[key] = new Backbone.Collection(value);
            } 
        });
        return resp;
    },

    // serializes the data without any helper methods
    toJSON: function() {
        // clone all attributes
        var attributes = _.clone(this.attributes);

        // go through each attribute
        $.each(attributes, function(key, value) {
            // check if we have some nested object with a toJSON method
            if (_.has(value, 'toJSON')) {
                // execute toJSON and overwrite the value in attributes
                attributes[key] = value.toJSON();
            } 
        });

        return attributes;
    }

});

The problem is now at the second part in toJSON.
For some reason

_.has(value, 'toJSON') !== true

doesn’t return true

Could somebody tell me what is going wrong?

  • 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-09T22:45:02+00:00Added an answer on June 9, 2026 at 10:45 pm

    Underscore’s has does this:

    has _.has(object, key)

    Does the object contain the given key? Identical to object.hasOwnProperty(key), but uses a safe reference to the hasOwnProperty function, in case it’s been overridden accidentally.

    But your value won’t have a toJSON property since toJSON comes from the prototype (see http://jsfiddle.net/ambiguous/x6577/).

    You should use _(value.toJSON).isFunction() instead:

    if(_(value.toJSON).isFunction()) {
        // execute toJSON and overwrite the value in attributes
        attributes[key] = value.toJSON();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement some sort of API on tornado and I have such
We're trying to implement some sort of Chess game and we have defined an
I am trying to implement a sort of reverse terminal: start server.py on some
I'm trying to implement some custom model metadata in ASP.NET MVC 3. I can't
I'm trying to implement some sort of 'just-for-me' game engine and the problem's plot
is it possibly in c# to have some sort of base class functionality which
I'm trying to implement some STL-style sorting algorithms. The prototype for std::sort looks something
Basically I'm trying to implement some sort of poor man's Aspect Oriented Programming in
I am trying to implement some sort of MVC in Java. Actually it's more
I'm trying to implement some sort of auditing in a Java EE JPA (2.0)

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.