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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:21:25+00:00 2026-06-09T14:21:25+00:00

I’m trying to use Backbone with an API. The default API response format is:

  • 0

I’m trying to use Backbone with an API.

The default API response format is:

{
somemetadatas:xxx , 
results:yyy
}

Whether it’s a fetch for a single model or a collection.

So as far as I know I can override the Backbone parse function with:

parse: function (response) {
    return response.results;
},

But I’ve seen in the documentation:

parse collection.parse(response)

parse is called by Backbone whenever
a collection’s models are returned by the server, in fetch. The
function is passed the raw response object, and should return the
array of model attributes to be added to the collection. The default
implementation is a no-op, simply passing through the JSON response.
Override this if you need to work with a preexisting API, or better
namespace your responses. Note that afterwards, if your model class
already has a parse function, it will be run against each fetched
model.

So if I have a response for a collection fetch like that:

{
somemetadatas:xxx , 
results:[user1,user2]
}

The first parse function on the collection will extract [user1,user2].

But the doc says:

Note that afterwards, if your model class
already has a parse function, it will be run against each fetched
model.

So it will try to find response.results; on user1 and user2

I need both parse functions on the model and collection because both model and collection datas will be under the result attribute.

But if i fetch on a collection, I don’t want the model parse function to be used againt a single array element.


So is there a solution to this problem?

I think of a solution where my collection parse function will transform something like this:

{
somemetadatas:xxx , 
results:[user1,user2]
}

into something like this:

[ {results.user1} , {results.user2} ]

So that the model parse function will not fail on a collection fetch.
But it’s a bit hacky… is there any elegant solution to this problem?


By the way, as my API will always produce results of this form, is it possible to override by default the parse function of all my models and collections? (Sorry i’m a JS noob…)

  • 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-09T14:21:27+00:00Added an answer on June 9, 2026 at 2:21 pm

    You could test if the data you receive is wrapped by a results member and react accordingly. For example,

    var M = Backbone.Model.extend({
        parse: function (data) {
            if (_.isObject(data.results)) {
                return data.results;
            } else {
                return data;
            }
        }
    });
    

    And a Fiddle http://jsfiddle.net/9rCH3/

    If you want to generalize this behavior, either derive all your model classes from this base class or modify Backbone’s prototype to provide this function :

    Backbone.Model.prototype.parse = function (data) {
        if (_.isObject(data.results)) {
            return data.results;
        } else {
            return data;
        }
    };
    

    http://jsfiddle.net/9rCH3/1/

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.