donburi-modeldeprecated

Simple model module for JS applications

Usage no npm install needed!

<script type="module">
  import donburiModel from 'https://cdn.skypack.dev/donburi-model';
</script>

README

Donburi Model Build Status

Made by taking ideas from facebook/flux Dispatcher and the example Store and Action implementations, this module attempts to bring a simple single Model for managing application state.

The idea is to store application in one object, to provide a single source of truth. All handling functions are called by a request method and are run synchronously.

Usage

var getModel = require('donburi-model').getModel;
var Constants = require('./Constants');

function addOne(operand) {
  return operand + 1;
}

var Model = getModel({
  counter: 0
});

Model.register(Constants.ADD_SOMETHING, function () {
  var state = Model.getState();
  // controlled mutation by getting a result from a pure function
  state.counter = addOne(state.counter);
  Model.update();
});
Model.register(Constants.REMOVE_SOMETHING, function () {
  Model.getState().counter -= 1;
  Model.update();
});

module.exports = Model;

Examples

See the tests in modules/__tests__/DonburiModel.js and the example application in examples/clicker.

Demo

See this JSBin for a demo.

References

facebook/flux