Permalink
Browse files

Presenters forward (and namespace) events from models

  • Loading branch information...
1 parent aa2e43f commit c39055dcb9ef3657e25cb052c4e482afd4ac9cac Mark Evans committed Mar 21, 2012
Showing with 19 additions and 0 deletions.
  1. +3 −0 src/presenter.coffee
  2. +16 −0 test/src/presenter.coffee
View
@@ -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
@@ -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()
+

0 comments on commit c39055d

Please sign in to comment.