Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Created April 24, 2012 04:49
Show Gist options
  • Save davidrichards/2476515 to your computer and use it in GitHub Desktop.
Save davidrichards/2476515 to your computer and use it in GitHub Desktop.
Using Backbone.js with a Mediator
# This is borrowed from http://bit.ly/K4h5xl
window.Support = (window.Support || {})
window.Support.Mediator = (params) ->
self = {}
self.register = (module) ->
module.subscribe = self.subscribe
module.publish = self.publish
true
self.subscribe = (channel, fn) ->
self.channels[channel] = [] unless self.channels[channel]
self.channels[channel].push(context: self, callback: fn)
self
self.publish = (channel) ->
return false unless self.channels[channel]
args = Array.prototype.slice.call(arguments, 1)
_.each(self.channels[channel], (subscription) ->
subscription.callback.apply(subscription.context, args)
)
self
# ==================
# = Initialization =
# ==================
params = (params || {})
self.channels = {}
# ================
# = Return Value =
# ================
self
window.App.Views.Demo.Show = Backbone.View.extend
initialize: ->
_.bindAll this, 'render'
@model.bind('all', @render)
@sections = new window.App.Collections.Sections(@model.get('sections'))
@mediator = new window.Support.Mediator()
@render()
render: ->
@top_navigation = new window.App.Views.Demo.TopNavigation(mediator: @mediator)
@page_navigation = new window.App.Views.Demo.PageNavigation(
model: @model
mediator: @mediator
sections: @sections
)
@page_header = new window.App.Views.Demo.PageHeader(model: @model, mediator: @mediator)
@pick_list = new window.App.Views.Demo.PickList(
model: @model
mediator: @mediator
sections: @sections
)
@referral_letter = new window.App.Views.Demo.Letter(
model: @model
mediator: @mediator
sections: @sections
)
@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment