Fire the remove event when fetching a collection #359

Closed
Vrael opened this Issue Jun 6, 2013 · 1 comment

2 participants

@Vrael

This is an odd behavior I think. I have two "section" instances. Each one with an exercises collection. Then, I do a fetch for each collection and here is the problem. From server can I to receive some model that can be in the two collections at the same time. But this wouldn't be a problem because they are independent instances.

Model:

class App.Models.Section extends Backbone.RelationalModel

  relations: [
    {
      type: Backbone.HasMany
      key: 'exercises'
      relatedModel: 'App.Models.Exercise'
      collectionType: 'App.Collections.Exercises'
      reverseRelation:
        key: 'section'
        includeInJSON: false
    }
  ]

View:

class App.Views.Section extends Backbone.Views

  initialize: ->
    @collection.bind 'add', @renderExercise
    @collection.bind 'remove', @unrenderExercise

    @subviews = {}

  renderExercise: (exercise) =>
    view = new Baskeitor.Views.ExerciseShow model: exercise
    @subviews{exercise.cid} = view
    @$el.append view.render().el

  unrenderExercise: (exercise) =>
     @subviews{exercise.cid}.remove()
     delete @subviews{exercise.cid}

Two instances:

section1 = new App.Models.Section
section2 = new App.Models.Section

Fetch in the two exercises collection:

section1.get('exercises').fetch({ data: params, remove:false })
section2.get('exercises').fetch({ data: params, remove:false })

I lied, this is my problem with Backbone. In a first time the collections receive their models and I generated a view for each model (an event 'add', so I render the exercise view). But next, for some reason I don't understand, Backbone trigger a remove event and removes all models repeated. In resume, only I can have in the collections whose models aren't in the other.

@Vrael

I have identified the problem. The matter is that ids are duplicates. If I change their ids manually, then all works fine. But otherwise it doesn't do it. But I think this don't have sense because I am instanciating two differents sections. Each section would have its own array with the ids of exercises.

@bpatram bpatram closed this Mar 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment