Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/api/presenter.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,22 @@ class HelloWorldPresenter extends Presenter {
}
}
```

### getRepo(repo, props)

Runs before assigning a repo to a Presenter. This method is given the
parent repo, either passed in via `props` or `context`. By default, it
returns a fork of that repo, or a new Microcosm if no repo is
provided.

This provides an opportunity to customize the repo behavior for a
particular Presenter. For example, to circumvent the default Presenter
forking behavior:

```javascript
class NoFork extends Presenter {
getRepo (repo) {
return repo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about an inline comment clarifying that this is a new Microcosm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same repo given to the Presenter (unless no repo is given). Is that what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh ok, ignore this one. I was thinking something different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

}
}
```
8 changes: 6 additions & 2 deletions src/addons/presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function Presenter (props, context) {
inherit(Presenter, BaseComponent, {
constructor: Presenter,

getRepo (repo, props) {
return repo ? repo.fork() : new Microcosm()
},

_setRepo (repo) {
this.repo = repo

Expand Down Expand Up @@ -152,9 +156,9 @@ inherit(PresenterContext, BaseComponent, {
},

getRepo () {
const repo = this.props.repo || this.context.repo
const { presenter, parentProps, repo } = this.props

return repo ? repo.fork() : new Microcosm()
return presenter.getRepo(repo || this.context.repo, parentProps)
},

updatePropMap ({ presenter, parentProps, parentState }) {
Expand Down
17 changes: 17 additions & 0 deletions test/addons/presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,23 @@ describe('::render', function () {

})

describe('::getRepo', function () {

it('can circumvent forking behavior', function () {
class NoFork extends Presenter {
getRepo (repo) {
return repo
}
}

let repo = new Microcosm()
let wrapper = mount(<NoFork repo={repo} />)

expect(wrapper.instance().repo).toEqual(repo)
})

})

describe('intents', function() {

test('receives intent events', function () {
Expand Down