I am trying to trigger one event after a batch of models have been added to a collection via the set
method. I overrode the set
method, called its parents set method and put a console.log in there, and it seemed to be triggering for every model that was trying to be added into the colleciton.
class MyCollection extends Backbone.Collection
set: ->
super
console.log('set called')
I could not determine why this was happening, but to move forward I decided to use underscores debounce
method to group these together and fire an event afterwards. This is what I came up with
class MyCollection extends Backbone.Collection
initialize: ->
@batchComplete = _.debounce(@_batchComplete, 1000)
super
@
_batchComplete: =>
if not window.test
window.test = @
if window.test is @
console.log 'SAME AS PREVIOUS'
else
console.log 'DIFFERENT', window.test, @
window.test = @
@trigger('setComplete')
set: ->
super
@batchComplete()
@
I have created an instance of this collection and trying to use the set method
c = new MyCollection
c.set([{...},{...},{...}])
My expectation of this was for window.test
to equal @
however they seem to be referencing different locations. This is a sample output in the console
> DIFFERNT MyCollection, MyCollection
> DIFFERNT MyCollection, MyCollection
I dont understand why window.test
is not the same as @
. As a result of each iteration not being the same, it means that it is not calling the same debounce
method