Skip to content

Instantly share code, notes, and snippets.

@bruth
Created August 2, 2011 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruth/1120062 to your computer and use it in GitHub Desktop.
Save bruth/1120062 to your computer and use it in GitHub Desktop.
Backbone Controller/Delegate Example
AppState = new Backbone.Model
domain: null
class Domain extends Backbone.Model
class DomainCollection extends Backbone.Collection
model: Domain
initialize: ->
AppState.bind 'change:domain', @changeDomain
# delegator/controller responsibilities..
changeDomain: (state, model, options) ->
if (previous = state.previous('domain'))
previous.trigger 'deactivate'
model.trigger 'activate'
class DomainView extends Backbone.View
events:
'click': 'click'
initialize: ->
@model.bind 'activate', @activate
@model.bind 'deactivate', @deactivate
activate: => $(@el).addClass 'active'
deactivate: => $(@el).removeClass 'active'
click: -> AppState.set 'domain', @model
class DomainCollectionView extends Backbone.View
el: '#domains'
initialize: ->
# some bind to listeners to the collection it represents..
@collection.bind 'reset', @reset
add: (collection, model, options) =>
view = new DomainView
model: model
$(@el).append view.render().el
reset: (collection, model, options) =>
collection.each @add
domains = new DomainCollection
$ ->
new DomainCollectionView
collection: domains
domains.fetch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment