2014-01-03 10:32:13 -08:00
# Template for classes with common functions, like hooking into the Mediator.
utils = require ' ./utils '
classCount = 0
2014-05-10 18:24:50 -07:00
makeScopeName = -> " class-scope- #{ classCount ++ } "
2014-03-24 14:27:10 -07:00
doNothing = ->
2014-01-03 10:32:13 -08:00
module.exports = class CocoClass
2014-05-10 18:24:50 -07:00
@nicks: [ ]
@nicksUsed: { }
@remainingNicks: [ ]
@nextNick: ->
2014-07-01 10:16:26 +08:00
return ( @ name or ' CocoClass ' ) + ' ' + classCount unless @ nicks . length
2014-05-10 18:24:50 -07:00
@remainingNicks = if @ remainingNicks . length then @ remainingNicks else @ nicks . slice ( )
baseNick = @ remainingNicks . splice ( Math . floor ( Math . random ( ) * @ remainingNicks . length ) , 1 ) [ 0 ]
i = 0
while true
nick = if i then " #{ baseNick } #{ i } " else baseNick
break unless @ nicksUsed [ nick ]
i ++
@ nicksUsed [ nick ] = true
nick
2014-01-03 10:32:13 -08:00
subscriptions: { }
shortcuts: { }
2014-02-15 17:29:54 -08:00
2014-01-03 10:32:13 -08:00
# setup/teardown
constructor: ->
2014-05-10 18:24:50 -07:00
@nick = @ constructor . nextNick ( )
2014-01-03 10:32:13 -08:00
@subscriptions = utils . combineAncestralObject ( @ , ' subscriptions ' )
@shortcuts = utils . combineAncestralObject ( @ , ' shortcuts ' )
@ listenToSubscriptions ( )
@scope = makeScopeName ( )
@ listenToShortcuts ( )
_ . extend ( @ , Backbone . Events ) if Backbone ?
destroy: ->
# teardown subscriptions, prevent new ones
@ stopListening ? ( )
2014-05-20 09:19:31 -07:00
@ off ? ( )
2014-01-03 10:32:13 -08:00
@ unsubscribeAll ( )
@ stopListeningToShortcuts ( )
2014-05-10 18:24:50 -07:00
@ constructor . nicksUsed [ @ nick ] = false
2014-02-14 13:31:26 -08:00
@ [ key ] = undefined for key of @
@destroyed = true
2014-03-24 14:27:10 -07:00
@off = doNothing
@destroy = doNothing
2014-01-03 10:32:13 -08:00
# subscriptions
2014-02-15 17:29:54 -08:00
2014-01-03 10:32:13 -08:00
listenToSubscriptions: ->
# for initting subscriptions
return unless Backbone ? . Mediator ?
for channel , func of @ subscriptions
func = utils . normalizeFunc ( func , @ )
Backbone . Mediator . subscribe ( channel , func , @ )
addNewSubscription: (channel, func) ->
# this is for adding subscriptions on the fly, rather than at init
return unless Backbone ? . Mediator ?
return if @ destroyed
return unless @ subscriptions [ channel ] is undefined
func = utils . normalizeFunc ( func , @ )
@ subscriptions [ channel ] = func
Backbone . Mediator . subscribe ( channel , func , @ )
unsubscribeAll: ->
2014-05-20 09:19:31 -07:00
return unless Backbone ? . Mediator ?
2014-01-03 10:32:13 -08:00
for channel , func of @ subscriptions
func = utils . normalizeFunc ( func , @ )
Backbone . Mediator . unsubscribe ( channel , func , @ )
2014-02-15 17:29:54 -08:00
2014-01-03 10:32:13 -08:00
# keymaster shortcuts
listenToShortcuts: ->
return unless key ?
for shortcut , func of @ shortcuts
func = utils . normalizeFunc ( func , @ )
key ( shortcut , @ scope , _ . bind ( func , @ ) )
2014-02-15 17:29:54 -08:00
2014-01-03 10:32:13 -08:00
stopListeningToShortcuts: ->
return unless key ?
2014-02-15 17:29:54 -08:00
key . deleteScope ( @ scope )