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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:48:39+00:00 2026-06-12T18:48:39+00:00

Using Backbone.js, I have a collection that I instanctiate, X . After instantiating it,

  • 0

Using Backbone.js, I have a collection that I instanctiate, X. After instantiating it, I immediately called X.fetch() such that the data can be loaded from the server as soon as possible. I then pass X into various views that will operate on the collection.

What is the best way for these views do differentiate between when the collection is simply loading (and thus empty) and when it is loaded but actually empty? I would like my views to show appropriate “loading…” text up until the collection has pinged the server. At which point, I would like them to say “It looks like there’s nothing here. Perhaps you should add something.”

I was thinking of maybe listening to the reset event on the collection in each respective view, but this seems rather fragile to me. What happens if the reset event has already fired before the view attaches its listener? Is there a good pattern to inspect the state of a collection to find out if it’s been fetched?

  • 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-12T18:48:40+00:00Added an answer on June 12, 2026 at 6:48 pm

    fetch method is asynchronous based on asynchronous nature of AJAX.
    Therefore you need to implement an event-driven behavior.

    X.fetch({
        success: function(collection, response) {
            hidePreLoader();
            renderViews(collection); // collection argument is your X collection.
        }
    });
    

    Update

    Based on your comments to my answer I can propose another idea.
    Namely you can set

    X.isLoaded = true;

    or any other property like

    X.loadedAt = Date.now();

    in success callback, so other code can check for the state of this property.

    But though I see a kind of bad design here.
    You can render your views with preloader showed and in success callback trigger some event on which your views will start to work with the collection since it’s loaded and became ready to use.
    So in total I’m again propose you to use event-driven behavior.

    I didn’t test, but here is a representation of my idea:

    var XCollection = Backbone.Collection.extend({
    
        // ...
    
        model: X,
        loadState: {},
        loadedAt: -1,
        initialize: function(options) {
            _.extend(this.loadState, Backbone.Events);
        },
    
        isLoaded: function() {
            return this.loadedAt > -1;
        }
    });
    
    var Subview = Backbone.View.extend({
    
        // ...
    
        initialize: function(options) {
            this.collection.loadState.on("loadComplete",
                this.onLoadComplete, this);
        },
    
        onLoadComplete: function(response) {
            this.hidePreloader();
            this.renderData();
        },
    
        /**
         * Checks is the collection loaded and view can render fetched models.
         * It's just an example.
         * You'll not need to use it if you're handling a loadComplete event.
         */
        isRenderingAllowed: function() {
            return this.collection.isLoaded();
        }
    });
    
    var XView = Subview.extend({
    
        // ...
    
        initialize: function(options) {
            Subview.prototype.initialize.apply(this, arguments);
        }
    });
    
    var YView = Subview.extend({
    
        // ...
    
        initialize: function(options) {
            Subview.prototype.initialize.apply(this, arguments);
        }
    });
    
    
    // ...
    
    var x = new XCollection();
    
    var xView = new XView({collection: x}),
        yView = new YView({collection: x});
    
    x.fetch({
        success: function(collection, response) {
            collection.loadedAt = Date.now();
            collection.loadState.trigger("loadComplete", response);
        }
    });
    

    Documentation

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

Sidebar

Related Questions

I have a collection ( using the Backbone.paginator ) that fetchs a array of
I'm using backbone.js, and I've got a collection with does fetch() sometimes. I don't
I have a web application that is currently using backbone.js 0.5.3 with backbone local
I'm using backbone for an app that I'm building. In this app, I have
Using Backbone.js, I use Collections to fetch and synchronize data between the client and
I'm using codebrew\backbone-rails in a nested model example (say I have a collection of
I have a super simple Backbone model/collection that wraps around a facebook feed. window.Story
I have a page that creates a table using backbone.js. each table row is
I have been using Backbone.js to act as a middleman between Wordpress (using the
I have a View (created using Backbone.View.extend) and a JSON object, I actually get

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.