Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Presenters forward (and namespace) events from models

  • Loading branch information...
commit c39055dcb9ef3657e25cb052c4e482afd4ac9cac 1 parent aa2e43f
@markevans authored
Showing with 19 additions and 0 deletions.
  1. +3 −0  src/presenter.coffee
  2. +16 −0 test/src/presenter.coffee
View
3  src/presenter.coffee
@@ -2,6 +2,9 @@ class egg.Presenter extends egg.Base
@init (opts)->
@presentedItems = opts.present || throw("#{@constructor.name} needs a 'present' option")
+ @onFirstSubscribe ->
+ for name, item of @presentedItems
+ @subscribe item, '*', (params, event) => @emit("#{name}:#{event.name}", params)
@decorate: (className, methodLists...)->
@jsonGenerators()[className] = (obj)->
View
16 test/src/presenter.coffee
@@ -52,3 +52,19 @@ describe 'egg.Presenter', ->
monkey = Monkey.create attrs: {hungry: 'yes'}
presenter = TestPresenter.create(present: {monkey: monkey})
expect( presenter.toJSON() ).toEqual monkey: {super: 'yesduper'}
+
+ it "should forward events", ->
+ monkey = Monkey.create attrs: {hungry: 'yes'}
+ presenter = TestPresenter.create(present: {monkey: monkey})
+ spyOn(presenter, 'emit')
+ presenter.on 'anything', ->
+ monkey.set('hungry', 'pig')
+ expect( presenter.emit ).toHaveBeenCalledWith('monkey:change.hungry', instance: monkey, from: 'yes', to: 'pig')
+
+ it "should not forward events if nothing has yet subscribed to it", ->
+ monkey = Monkey.create attrs: {hungry: 'yes'}
+ presenter = TestPresenter.create(present: {monkey: monkey})
+ spyOn(presenter, 'emit')
+ monkey.set('hungry', 'pig')
+ expect( presenter.emit ).not.toHaveBeenCalled()
+
Please sign in to comment.
Something went wrong with that request. Please try again.