Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fake implementation of inter component PubSub mechanism
EventeableMixin = Ember.Mixin.create
# Handle eventBroker change, tricky one!
init: ->
if !@get('eventBroker')
@set('eventBroker', create_local_event_broker)
register_events_listeners()
register_events_listeners: ->
broker = @get('eventBroker')
list = @get('events_listeners')
for event, action in list
broker.register(event, action)
events_send: (name) ->
# Does the matching and trigger the action on the broker
# NOTE: When unspecified, channel is always the most qualified available.
# Ex: for PurchaseComponent, @emit(event: 'submit') will default to channel: "components:purchase:INSTANCE_ID"
ParentComponent = Ember.Component.extend EventeableMixin
events_receivers: [
{ event: 'validation_failed', channel: 'components:auth:*', action: 'validation_failed' }
{ event: 'validation_failed', channel: 'components:purchase:*', action: 'validation_failed' }
{ event: 'user_authentication_failed', channel: 'components:auth:*', action: 'validation_failed' }
{ event: 'subscription_purchase_failed', channel: 'components:purchase:*', action: 'vaildation_failed' }
{ event: 'user_authentication_succeeded', channel: 'components:auth:*', action: 'purchase' }
{ event: 'subscription_purchase_succeeded', channel: 'components:purchase:*', action: 'redirect' }
]
actions:
validation_failed: ->
@emit event: 'loading', channel: '*', arguments: [false]
purchase: ->
@emit event: 'submit', channel: 'components:purchase'
redirect: ->
# Purchase succeeded
redirect 'somewhere'
submit: ->
if authenticated
@purchase()
else
@emit event: 'submit', channel: 'components:auth-component'
# In parent component template:
# = auth-component
# = purchase-component
AuthComponent = Ember.Component.extend EventeableMixin
events_emitters: [
'user_authentication_succeeded'
'user_authentication_failed'
'validation_suceeded'
'validation_failed'
]
events_receivers: [
{ event: 'submit', action: 'validate' }
{ event: 'validated', action: 'authenticate' }
{ event: 'udpdate_loading_state', action: 'setLoading' }
]
actions:
authenticate: ->
# ACTION: Authenticate user
if successfull
@emit event: 'user_authentication_succeeded'
else
@emit event: 'user_authentication_failed'
validate: ->
# ACTION: Run validation
if successfull
@emit event: 'validation_succeeded'
else
@emit event: 'validation_failed'
set_loading: (state) ->
@set 'loading', state
PurchaseComponent = Ember.Component.extend EventeableMixin
events_emitters: [
'purchase_succeeded'
'purchase_failed'
'validation_suceeded'
'validation_failed'
]
events_receivers: [
{ event: 'submit', action: 'validate' }
{ event: 'validated', action: 'purchase' }
{ event: 'udpdate_loading_state', action: 'setLoading' }
]
actions:
purchase: ->
# ACTION: Purchase Subscription
if successfull
@sendEvent 'purchase_succeeded'
else
@sendEvent 'purchase_failed'
validate: ->
# ACTION: Run validation
if successfull
@sendEvent 'validation_succeeded'
else
@sendEvent 'validation_failed'
set_loading: (state) ->
@set 'loading', state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.