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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:14:15+00:00 2026-06-16T01:14:15+00:00

I am trying to toggle a state variable in my Backbone collection (Posts) after

  • 0

I am trying to toggle a state variable in my Backbone collection (“Posts”) after a certain period of time (called from a view), and am trying to use setTimeout. However, I think I am screwing up my scope as my toggle function is not working (it is getting called, but it is not changing properly).

If I use

setTimeout(this.model.collection.toggleReadyToPreloadNewPost, 1000);

, the code does not work, while if I use

this.model.collection.toggleReadyToPreloadNewPost();

it toggles it correctly. I was wondering how I can solve this?

Backbone View

//ensures namespace is not already taken yet
wedding.views = wedding.views || {};

//each PostView corresponds to a single post container 
//which contains the user name, user comment, and a photo
wedding.views.PostView = Backbone.View.extend({

  tagName: "li",
  template: "#item-template",
  className: "hideInitially post",

  initialize: function() {
    _.bindAll(this);
    this.template = _.template($(this.template).html());
  },

  render: function() {
    this.preload();
    return this;
  },

  //preloads an image and only after it is done, then append
  preload: function() {
    var image = new Image(); 
    image.src = this.model.get("src");
    this.model.incrementTimesSeen();

    //hides the element initially, waits for it to finish preloading, then slides it down
    //updates lastSeen only after the image is displayed
    image.onload = $.proxy(function() {
      var html = this.template( {model: this.model.toJSON()} );

      this.model.setLastSeen();

      //shows the image by sliding down; once done, remove the hideInitially class
      this.$el.hide().append(html).slideDown();
      this.$el.removeClass("hideInitially");

      setTimeout(this.model.collection.toggleReadyToPreloadNewPost, 1000);
    }, this);
  }

});

Backbone Collection

//check if namespace is already occupied
wedding.collections = wedding.collections || {}; 

wedding.collections.Posts = Backbone.Collection.extend({
  model: wedding.models.Post,

  initialize: function() {
    this.readyToPreloadNewPost = 1;
  },

  //toggles "readyToPreloadNewPost" between 1 and 0 
  toggleReadyToPreloadNewPost: function() {

    this.readyToPreloadNewPost = this.readyToPreloadNewPost ? 0 : 1;
  }    
});
  • 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-16T01:14:16+00:00Added an answer on June 16, 2026 at 1:14 am

    When you do this:

    setTimeout(this.model.collection.toggleReadyToPreloadNewPost, 1000);
    

    You’re just handing setTimeout the plain unbound toggleReadyToPreloadNewPost function and setTimeout will call it as a simple function. The result is that this will be window inside toggleReadyToPreloadNewPost when setTimeout calls the function.

    You can get the right this by wrapping your method call in an anonymous function:

    var _this = this;
    setTimeout(function() {
        _this.model.collection.toggleReadyToPreloadNewPost();
    }, 1000);
    

    You can also use _.bind:

    setTimeout(
        _(this.model.collection.toggleReadyToPreloadNewPost).bind(this.model.collection),
        1000
    );
    

    You could also use _.bindAll inside the collection’s initialize to always bind that method to the appropriate this:

    wedding.collections.Posts = Backbone.Collection.extend({
        initialize: function() {
            _.bindAll(this, 'toggleReadyToPreloadNewPost');
            //...
        }
    

    And then your original

    setTimeout(this.model.collection.toggleReadyToPreloadNewPost, 1000);
    

    should do the right thing. You’d only want to go this route if you always wanted toggleReadyToPreloadNewPost to be bound, I’d probably go with the first one instead.

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

Sidebar

Related Questions

I'm trying to stop a toggle button from changing state when clicked, based on
I'm trying to get the checked state of a checkbox after it's changed. When
I'm trying to toggle the visibility of certain DIV elements on a website depending
I'm trying to create a 3 state toggle button by overriding the Button class.
I'm trying to toggle some divs and at the same time set an active
I am trying the distinguish the state of the toggle button when clicked. I
I am trying to setup a custom toggle button where I use an Imagebutton
i am trying to invert a boolean value (toggle); var current_state=$.cookie('state'); if(current_state==null) { current_state=false;
HI al I'm trying to allow a kind of 'toggle' state on a bunch
Im trying to toggle an animation on height when a button Show Details/Hide Details

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.