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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:39:56+00:00 2026-05-29T22:39:56+00:00

I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //

  • 0

I have the following Backbone.js model and collection:

// Model 
lr.Event = Backbone.Model.extend({});

// Collection
lr.Events = Backbone.Collection.extend({
  model: lr.Event,
  initialize: function(models, options) {
    this.date = options.date;
  },
  url: function() {
    return '/events' + '?date=' + this.date.toString('yyyy-MM-dd');
  }  
});

Which are used with a View and its sub-view:

// View
lr.MapView = Backbone.View.extend({
  el: $('#wrapper'),
  initialize: function() {
    this.date = this.options.date;
    _.bindAll(this, "render", "addAllEvents", "addOneEvent", "showNextDay", "collectData");
    this.collectData();
  },
  events: {
    "click #next": "showNextDay",
  },
  collectData: function() {
    var that = this;
    if (this.collection) this.collection.reset();
    this.collection = new lr.Events([], { date : this.date });
    this.collection.fetch({
      success:  function(resp) {
        that.render();
        that.addAllEvents();
      }
    });      
  },
  showNextDay: function() {
    this.date = this.date.add(1).days();
    this.collectData();
  },
  addAllEvents: function() {
    this.collection.each(this.addOneEvent);
  },
  addOneEvent: function(e) {
    var ev = new lr.EventView({ 
      model:  e,
      parentView: this
    });
  },
  ...
});

// Sub-view
lr.EventView = Backbone.View.extend({
  initialize: function() {
    var that = this;
    this.model.bind('change', function() {
      console.log('foo');
      that.remove();
    });
  }
  ...
});

I am trying to bind the sub-view to changes to its associated model. So I would expect that when I call this.collection.reset(), console.log('foo') would be called. But it is not. I have console.loged the collection before and I after I reset it, and it is definitely going from “something to nothing,” so I assume I am somehow messing up the binding of the sub-view to the model.

Anyone see the error?

  • 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-29T22:39:57+00:00Added an answer on May 29, 2026 at 10:39 pm

    Calling reset() with no models empties the collection, but does not ‘change’ the existing models. The ‘change’ event occurs when some attribute of the model changes. Calling reset() does trigger the ‘reset’ event on the collection itself (also triggered when you fetch a collection); when this is fired, you should also reset your UI, creating and rendering all the sub-view items (in your case, it looks like you would call render and addAllEvents when you get the reset event).

    this.collection.on('reset', this.removeExistingEventViewsThenAddAllEvents, this);
    

    Now, when some attribute of an Event model does change, you will hit your change handler:

    this.model.bind('change', function() {
      console.log('foo');
      that.remove();
    });
    

    But, I really doubt you want to remove the Event view when the model changes. More likely, you want to re-render it, or update some part of it using JQuery.

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

Sidebar

Related Questions

Using the following as an example: var Case = Backbone.Model.extend({ initialize: function(){ this.bind(error, function(model,
I have the following collection: UsersMod.users_list = Backbone.Collection.extend({ add : function(model) { var duplicate
I have a view and collection like this: window.DmnView = Backbone.View.extend({ template: _.template($(#tmpl_dmnListItem).html()), events:
Lets have the following code : ( function($) { TeacherModel = Backbone.Model.extend({ defaults :
I've got the following view file: var BucketTransferView = Backbone.View.extend( { initialize: function(args) {
I have the following Parent Object: Context = { ContextModel: Backbone.Model.extend({ //model Code }),
I have the following model: var Soq = Backbone.model.extend({ default:{ 'name': 'something' , 'parents':
I have the following: // MODEL NotificationModel = C.BB.Model.extend({ defaults : { read :
I have the following backbone.js controller: App.Controllers.PlanMembers = Backbone.Controller.extend({ routes: { message/:messageType: sendMessage, :
I have notifications in my Rails & Backbone.js app // MODEL NotificationModel = App.BB.Model.extend({

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.