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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:21:23+00:00 2026-05-29T09:21:23+00:00

I’m building a Backbone application and I’m observing some behaviour I can’t place. Consider

  • 0

I’m building a Backbone application and I’m observing some behaviour I can’t place. Consider the following Collection:

window.Pictures = Backbone.Collection.extend({
  model: Picture,
  url: 'latest.json',
  parse: function(response) {
    this.foobar = 1;
  },  
  fetchPage: function() {
    this.foobar = 2;
    return this;
  }
});

On a Chrome (or Firefox) console I’ve issued the following command:

> p = new Pictures(); p.fetch(); p.fetchPage();
> p.foobar
1

When I do:

> p = new Pictures(); p.fetch()
> p.fetchPage();
> p.foobar
2

I really don’t understand this. Why is the first execution different from the second execution?

  • 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-05-29T09:21:25+00:00Added an answer on May 29, 2026 at 9:21 am

    The fetch call is asynchronous because it involves an AJAX call to the server:

    fetch collection.fetch([options])
    Fetch the default set of models for this collection from the server, resetting the collection when they arrive.

    And fetch will call parse:

    parse collection.parse(response)
    parse is called by Backbone whenever a collection’s models are returned by the server, in fetch.

    So p.parse() may be called before or after p.fetchPage() depending on timing issues that are beyond your control.

    In the first case:

    > p = new Pictures(); p.fetch(); p.fetchPage();
    

    fetchPage is getting called before fetch gets its response from the server and gets around to calling parse so the calling sequence ends up like this this:

    1. You call p.fetch().
    2. AJAX call is made.
    3. You call p.fetchPage().
    4. AJAX response is received.
    5. The AJAX success handler calls p.parse().

    In the second case:

    > p = new Pictures(); p.fetch()
    > p.fetchPage();
    

    Enough time passes between the lines for the AJAX call to return before p.fetchPage() is called so things happen in the order expect purely by accident.

    If you need things to happen in a certain order then you’ll need to use the success (and possibly error) callback that fetch provides:

    The options hash takes success and error callbacks which will be passed (collection, response) as arguments.

    So this should give you a consistent result of 2:

    p = new Pictures();
    p.fetch({
        success: function(collection, response) {
            collection.fetchPage();
            console.log(collection.foobar);
        }
    });
    

    Of course, if fetchPage involves an AJAX call then you’d have to add yet another layer of callbacks to get a consistent foobar value.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.