Build simple stores for your reactive data.
This is a minimalistic approach to Flux to be used with React.
npm install phlux
Returns a PhluxStore, which includes methods for storing and retrieving reactive data.
Accepts an object as the protostore, whose properties will be copied into the store. If
the protostore includes data
, it will be accepted as the inital data for the store.
var Phlux = require("phlux")
Phlux.createStore()
// {
// data: {},
// trigger: function() {...},
// addListener: function() {...},
// listeners: [...]
// }
Phlux.createStore({
data: {
message: "Hello World!"
}
})
// {
// data: {
// message: "Hello World!"
// },
// trigger: function() {...},
// addListener: function() {...},
// listeners: [...]
// }
Phlux.createStore({
data: {
message: "Hello World!"
},
sayMessage: function() {
console.log(this.data.message)
}
})
// {
// data: {
// message: "Hello World!"
// },
// sayMessage: function() {
// console.log(this.data.message)
// },
// trigger: function() {...},
// addListener: function() {...},
// listeners: [...]
// }
Returns a mixin which will bind the state of a ReactComponent to the state of
a PhluxStore. Whenever store.trigger()
is called, the componet will be updated
such that component.state.label
returns store.data
.
var React = require("react")
var Phlux = require("phlux")
var DudeStore = Phlux.createStore({
data: {
name: "Andrew"
}
})
var Dude = React.createClass({
mixins: [
Phlux.connectStore(DudeStore, "dude")
],
render: function() {
return (
<div>
My name is {this.state.dude.name}
</div>
)
}
})
Called after the store has been constructed.
Phlux.createStore({
data: {
value: 2
},
initiateStore: function() {
this.data.value += 2
}
})
// {
// data: {
// value: **4**
// },
// initiateStore: function() {
// this.data.value += 2
// },
// trigger: function() {...},
// addListener: function() {...},
// listeners: [...]
// }
Stores all the data.
Executes all the listeners. Every listener is called with a copy of PhluxStore.data
.
Accepts a function as a listener, which will be called when PhluxStore.trigger()
is called.