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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:43:54+00:00 2026-06-16T00:43:54+00:00

I have a Backbone application where the JSON I get from the server isn’t

  • 0

I have a Backbone application where the JSON I get from the server isn’t exactly 1 on 1 with how I want my models to look. I use custom parse functions for my models, ex:

parse: function(response) {
   var content = {};
   content.id = response.mediaId;
   content.image = response.image.url;
   return content;
}

This works. But, in some cases I have an API call where I get lots of information at once, for instance, information about an image with its user and comments:

{
    "mediaId": "1",
    "image": {
        "title": "myImage",
        "url": "http://image.com/234.jpg"
    },
    "user": {
        "username": "John"
    },
    "comments": [
        {
            "title": "Nice pic!"
        },
        {
            "title": "Great stuff."
        }
    ]
}

How would I go about creating a new User model and a Comments collection from here? This is an option:

parse: function(response) {
   var content = {};
   content.id = response.mediaId;
   content.image = response.image.url;
   content.user = new User(response.user);
   content.comments = new Comments(response.comments);
   return content;
}

The trouble here is, by creating a new User or new Comments with raw JSON as input, Backbone will just add the JSON properties as attributes. Instead, I’d like to have an intermediate parse-like method to gain control over the objects’ structure. The following is an option:

parse: function(response) {
    // ...
    content.user = new User({
        username: response.user.username
    });
    // ...
}

…but that’s not very DRY-proof.

So, my question is: what would be a nice pattern to create several models/collections out of 1 JSON response, with control over the models/collections attributes?

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-16T00:43:55+00:00Added an answer on June 16, 2026 at 12:43 am

    It may not be the nicest way possible, but this is how I do it:

    content.user = new User(User.prototype.parse(response.user));
    

    The only problem is that the this context in User.parse will be wrong. If you don’t have any specific code in the User constructor, you can also do:

    content.user = new User();
    content.user.set(user.parse(response.user));
    

    I also noticed an interesting note in the Backbone version 0.9.9 change log:

    The parse function is now always run if defined, for both collections and models — not only after an Ajax call.

    And looking at the source code of Model and Collection constructor, they do it like so:

    if (options && options.parse) attrs = this.parse(attrs);
    

    Maybe upgrading to 0.9.9 will give you what you need? If upgrade is not an option, you can of course implement the same in your own constructor.

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

Sidebar

Related Questions

I have JavaScript application, where I use client-side templates (underscore.js, Backbone.js). Data for initial
i have a backbone.js api i want to use in an ExtJS app. Extjs
I'm trying to have my backbone application check the server as often as possible
I have a very complex Backbone application with many views/Models and collections. at times
In my Backbone application I have a model consisting of a couple of sub-models
I have an application in rails 3 using backbone.js When I use @model.save, i
Hello I need to make json requests from Backbone to an API (I have
I use Backbone.js and jQuery 1.7 in my application and I have some problems
I have a backbone application which, upon load, needs to fetch data from four
I have a Backbone.js application that allows users to create graphs based on large

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.