Created
August 2, 2011 12:02
-
-
Save bruth/1120062 to your computer and use it in GitHub Desktop.
Backbone Controller/Delegate Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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