Skip to content

Instantly share code, notes, and snippets.

@itzaks
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save itzaks/b5b3a422751ace1a95d3 to your computer and use it in GitHub Desktop.

Select an option

Save itzaks/b5b3a422751ace1a95d3 to your computer and use it in GitHub Desktop.
Add mediator style pub/sub to space-pen views using flesch/psst.js
psst = require 'psst'
module.exports = require 'space-pen'
module.exports.EventView =
class EventView extends module.exports.View
subscriptions: null
initialize: ->
return unless @subscriptions
for subscription, method of @subscriptions
method = @[method] if typeof method is 'string'
psst.on subscription, method
@beforeRemove = -> @unsubscribe()
emit: ->
psst.emit arguments...
unsubscribe: ->
for subscripition, method of subscriptions
psst.off subscription
@itzaks
Copy link
Author

itzaks commented Aug 21, 2014

Use:

{ EventView, View, $$, $ } = require './lib/space-talk'

class Mail extends EventView
  subscriptions:
    'mail:show'   : 'init_mail'
    'mail:export' : 'export'

  init_mail: => 
    @emit('mail:init')

  initialize: ->
    super()

  beforeRemove: ->
    super()

@itzaks
Copy link
Author

itzaks commented Aug 21, 2014

remember to call super on initialize and beforeRemove if used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment