Skip to content

Commit af3540b

Browse files
committed
Allow for multiple models in a single store
Remove the ability to avoid namespaces and allow for multiple models to be used in each barracks store
1 parent e5474cb commit af3540b

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

barracks-react.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
const React = require('react')
22
const barracks = require('barracks')
3-
const sid = require('shortid')
43

5-
function generateStore (component, model) {
4+
function generateStore (component, models) {
65
const BarracksReact = React.createClass({
76
componentWillMount: function () {
8-
if (!model.namespace) {
9-
model.namespace = sid.generate()
7+
if (!Array.isArray(models)) {
8+
models = [models]
109
}
11-
this.namespace = model.namespace
1210
this.store = barracks()
1311
this.store.use({
1412
onError: (err) => {
1513
this.setState({err})
1614
},
1715
onStateChange: (state, data, prev, caller) => {
18-
this.setState({model: state[this.namespace]})
16+
this.setState({data: state})
1917
}
2018
})
21-
this.store.model(model)
19+
const data = {}
20+
models.forEach((model) => {
21+
this.store.model(model)
22+
data[model.namespace] = model.state
23+
})
2224

2325
const createSend = this.store.start({subscriptions: false})
24-
const _send = createSend('reactDispatcher', true)
25-
const send = (action, msg) => {
26-
_send(`${this.namespace}:${action}`, msg)
27-
}
28-
this.setState({model: Object.assign({}, model.state), send})
26+
const send = createSend('reactDispatcher', true)
27+
this.setState({data, send})
2928
},
3029

3130
componentWillUnmount: function () {

test/test.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,38 @@ const testStore = {
2323
}
2424
}
2525

26+
const testStore2 = {
27+
namespace: 'boop',
28+
state: {
29+
header: 'beep'
30+
},
31+
reducers: {
32+
update: function (state, data) {
33+
return {header: data}
34+
}
35+
}
36+
}
37+
2638
const TestComponent = React.createClass({
2739
change: function (evt) {
2840
evt.preventDefault()
29-
this.props.send('update', 'foobar')
41+
this.props.send('foo:update', 'foobar')
3042
},
3143
render: function () {
3244
const props = {
33-
onClick: (evt) => this.props.send('update', 'foobar'),
34-
onBlur: (evt) => this.props.send('makeError')
45+
onClick: (evt) => this.props.send('foo:update', 'foobar'),
46+
onBlur: (evt) => this.props.send('foo:makeError')
3547
}
36-
return React.createElement('h1', props, `${this.props.err || this.props.model.header}`)
48+
return React.createElement('h1', props, `${this.props.err || this.props.data.foo.header}`)
49+
}
50+
})
51+
52+
const TestComponent2 = React.createClass({
53+
render: function () {
54+
const props = {
55+
onClick: (evt) => this.props.send('boop:update', 'plorb')
56+
}
57+
return React.createElement('h1', props, `${this.props.data.boop.header} ${this.props.data.foo.header}`)
3758
}
3859
})
3960

@@ -61,25 +82,15 @@ tape('basic', (t) => {
6182
}, 5)
6283
})
6384

64-
tape('no namespace', (t) => {
65-
const store = {
66-
state: {
67-
header: 'beep'
68-
},
69-
reducers: {
70-
update: function (state, data) {
71-
return {header: data}
72-
}
73-
}
74-
}
75-
const Test = bearact(TestComponent, store)
85+
tape('multimodel', (t) => {
86+
const Test = bearact(TestComponent2, [testStore, testStore2])
7687
const el = React.createElement(Test, {}, null)
7788
const wrapper = mount(el)
7889
const h1 = wrapper.find('h1')
79-
t.equal(h1.text(), store.state.header)
90+
t.equal(h1.text(), `${testStore2.state.header} ${testStore.state.header}`)
8091
h1.simulate('click')
8192
setTimeout(() => {
82-
t.equal(wrapper.find('h1').text(), 'foobar')
93+
t.equal(wrapper.find('h1').text(), 'plorb test')
8394
t.end()
8495
}, 5)
8596
})

0 commit comments

Comments
 (0)