Coherence
Models and Intents, for your React app. Inspired by Flux.
Usage
Installation
npm install coherence
var Coherence = require('coherence');
Coherence is provided as a Node module, and has been used with Browserify for
client-side code. If you've used another bundling tool for client-side usage,
let me know!
Intents
Intents describe things that somebody wants to happen in your app. "Somebody"
might be you, the programmer, or the user. "Things" might be choosing a
background color, sending a message, opening a form, or pre-fetching data from
the server.
Intents in Coherence are analogous to Actions and the Dispatcher(s) of Flux.
Intents are defined by a yielding function, which defines any input, and the
output payload provided to the intent's subscribers, whenever the Intent is
declared.
Defining Intents
var Coherence = require('coherence');
var Speak = Coherence.Intent((words) => {
return words;
});
module.exports = Speak;
var Coherence = require('coherence');
var ChooseAnimal = Coherence.Intent((animalId) => {
return animalId;
});
module.exports = ChooseAnimal;
Declaring Intents
When something should actually happen in your app, you declare an Intent.
speak: function() {
Speak(this.state.words);
},
Responding to Intents
Intent subscriptions are the implementations of your application's behavior, in
response to Intent declarations.
Speak.subscribe((words) => {
});
Coherence.LocationFactory: Optional pushState and replaceState support
If you'd like to use path-based Intents, you can use
Coherence.LocationFactory to define Intents that will update history, and the
URL, for you.
This allows your app to handle deep-linking, and browser back/forward, using
the same patterns used for any other activity in your application.
var Coherence = require('coherence');
var Location = Coherence.Location(window);
module.exports = Location;
Location Intents yield the path to subscribers, so that you can
bring your preferred routing solution.
Defining view state models
Models define component-bindable information. You can house your business logic
here, but anything complex should probably be pushed into classes of your own
creation.
For Intent declarations to effect changes in your views:
- your views should be bound to Models
- Intent subscriptions should eventually call your Models' methods
- your Models' methods should modify view-exposed state
var Model = require('coherence').Model;
var Speak = require('./speak');
var Animals = function(dependencies) {
var getAnimalAsync = dependencies.getAnimalAsync;
var model = Model(function(expose, def) {
var currentAnimal = expose('currentAnimal');
var said = expose('said');
def('select', function(animalId) {
getAnimalAsync.then(function(animal) {
currentAnimal.push(animal);
});
});
def('say', function(words) {
said.push(words);
});
});
Speak.subscribe((words) => {
animals.say(words);
});
return model;
};
module.exports = Animals;
Integrating Coherence Intents and Models with React
var animalStore = require('./animal_store').instance;
var Speak = require('./speak');
var animals = require('./animals-model').instance;
var AnimalView = React.createClass({
componentWillMount: function() {
this.animalBindings = animals.ReactBindings(this, animals, {
animalData: 'currentAnimal',
animalSays: 'says',
});
},
componentWillUnmount: function() {
this.animalBindings.unbind();
},
render: function() {
return (
<div>
<button onClick={this.saySomething}>Make Animal Say "something"</button>
<AnimalData data={this.state.animalData} />
<p>Animal says: {this.state.animalSays}</p>
</div>
);
},
saySomething: function() {
Speak('something');
},
});
Features
Binding a Model to a React component
Within the context of a React class definition:
Development
Add or modify tests to reflect the change you want.
Run them with npm test, and update the code in src/ till your tests pass.
Running the tests will run npm run compile, which outputs the code as ES5,
polyfilled via core-js.