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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:07:17+00:00 2026-06-07T08:07:17+00:00

I am using this.collection.each() to iterate through the collection fetched from the backend. Problem:

  • 0

I am using this.collection.each() to iterate through the collection fetched from the backend.

Problem: I notice that when I bind the reset event of the collection to the render method of the view in the initialize method and place a console.log() within this.collection.each, I see the console output as expected.

However, If I dont do the binding above, and simply use this.render() within initialize, the console.log() does not output anything. This seems really strange to me, can anyone provide an explaination?

I also placed a console.log(this.collection); just before the loop, and this always outputs the collection correctly! I was guessing that the collection has not been populated on initialization of the View, but that will cause console.log(this.collection); to not show anything.

This Works

SimilarPhotoListView = Backbone.View.extend({
    el: '#modal_similar_items',

    initialize: function() {
        this.collection.on('reset', this.render, this);
    },

    render: function() {
        console.log(this.collection);
        this.collection.each(function(photo, index) {
            console.log('hello');
        }, this);
        return this;
    }
});

This does not output from within this.collection.each()

SimilarPhotoListView = Backbone.View.extend({
    el: '#modal_similar_items',

    initialize: function() {
        this.render();
    },

    render: function() {
        console.log(this.collection);
        this.collection.each(function(photo, index) {
            console.log('hello');
        }, this);
        return this;
    }
});

Both classes are instantiated via:

renderSimilarPosts: function() {
    this.similarPhotoList = new SimilarPhotoCollection();
    this.similarPhotoListView = new SimilarPhotoListView({ collection: this.similarPhotoList });
    this.similarPhotoList.fetch({
        data: {post_id: this.model.id},
        processData: true
    });
}
  • 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-07T08:07:21+00:00Added an answer on June 7, 2026 at 8:07 am

    When you initialize your view, this.similarPhotoList is an empty collection. Therefore, when you create your similarPhotoListView, you’re passing it an empty collection. similarPhotoListView.initialize calls render thus with an empty collection, all before the collection is populated by fetch.

    The reason the first method works is because reset is triggered in collection.fetch. From the backbone source:

    fetch:
    ...
        options.success = function(resp, status, xhr) {
                collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
                if (success) success(collection, resp);
              };
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my problem: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1
I have a rake task that needs to iterate through a large number of
I've been working on a project where I need to iterate through a collection
http://www.dreamincode.net/forums/xml.php?showuser=335389 Given the XML above, how can I iterate through each element inside of
I'm using this code: Dim cListItems As New System.Collections.Generic.List(Of Combobox_values) cListItems.Add(New Combobox_values(One, 1)) cListItems.Add(New
Im using the python framework flask with sqlalchemy. My many-to-many looks like this: collections_questions
I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
This code using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data.Entity.ModelConfiguration;
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {
This C#/WPF code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using

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.