Skip to content

Instantly share code, notes, and snippets.

@patmaddox
Created November 8, 2011 23:11
Show Gist options
  • Select an option

  • Save patmaddox/1349631 to your computer and use it in GitHub Desktop.

Select an option

Save patmaddox/1349631 to your computer and use it in GitHub Desktop.
Trying to get a collection to pass through events from collections contained inside its models
class Seminar extends Backbone.Model
initialize: ->
@configureBubbleChildEvents()
bubbleEvent: (eventName, object) =>
@trigger eventName, object
configureBubbleChildEvents: ->
@get('students').bind('all', @bubbleEvent)
class Seminars extends Backbone.Collection
bubbleEvent: (eventName, object) =>
@trigger eventName, object
add: (object) ->
Backbone.Collection.prototype.add.call(this, object)
if _.isArray(object)
_(object).each (model) => model.bind('all', @bubbleEvent)
else
object.bind 'all', @bubbleEvent
remove: (object) ->
Backbone.Collection.prototype.remove.call(this, object)
if _.isArray(object)
_(object).each (model) => model.unbind('all', @bubbleEvent)
else
object.unbind 'all', @bubbleEvent
class Students extends Backbone.Collection
class Student extends Backbone.Model
# this one passes...
describe "Seminar", ->
it "bubbles up events from the students collection", ->
students = new Students()
seminar = new Seminar(students: students)
studentName = null
callback = (object) -> studentName = object.get('name')
seminar.bind 'add', callback
students.add new Student(name: 'Pat')
expect(studentName).toEqual('Pat')
# all working
describe "Seminars collection", ->
it "bubbles up events from the seminars", ->
students = new Students()
seminar = new Seminar(students: students)
seminars = new Seminars([seminar])
studentName = null
callback = (object) -> studentName = object.get('name')
seminars.bind 'add', callback
students.add new Student(name: 'Pat')
expect(studentName).toEqual('Pat')
it "bubbles up events from added seminars", ->
students = new Students()
seminar = new Seminar(students: students)
seminars = new Seminars()
seminars.add seminar
studentName = null
callback = (object) -> studentName = object.get('name')
seminars.bind 'add', callback
students.add new Student(name: 'Pat')
expect(studentName).toEqual('Pat')
it "does not bubble up events from seminars that have been removed", ->
students = new Students()
seminar = new Seminar(students: students)
seminars = new Seminars([seminar])
studentName = null
callback = (object) -> studentName = object.get('name')
seminars.bind 'add', callback
seminars.remove seminar
students.add new Student(name: 'Pat')
expect(studentName).toBe(null)
it "removing doesn't mess with other callbacks", ->
students1 = new Students()
seminar1 = new Seminar(students: students1)
students2 = new Students()
seminar2 = new Seminar(students: students2)
seminars = new Seminars([seminar1, seminar2])
studentName = null
callback = (object) -> studentName = object.get('name')
seminars.bind 'add', callback
seminars.remove seminar1
students1.add new Student(name: 'Pat')
expect(studentName).toBe(null)
students2.add new Student(name: 'Ralph')
expect(studentName).toEqual('Ralph')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment