Skip to content

v1.1.0

Compare
Choose a tag to compare
@mikaelbr mikaelbr released this 21 Jul 23:05
· 10 commits to master since this release

Additions:

decorator.observer(structure, fields, decoratee)

The observer decorator is a very useful one if you need horizontal data
dependencies. If you require one of your components to get injected data
automatically and update everytime that data changes.

Fields passed to observer should be defined as the following:

{
  namePassedAsProps: ['nested', 'key', 'path'],
  anotherNamePassedAsProps: 'shallowKeyPath'
}

Require the decorator by doing:

var observer = require('omnipotent/decorator/observer');
// or
var observer = require('omnipotent').decorator.observer;

Examples:

var structure = immstruct({ hero { name: 'Natalia Romanova' } });

var Title = component('View', ({hero}) => {name.deref()});

var ObservedTitle = observer(structure, {
  hero: ['hero', 'name'] // key path in structure
}, Title);

render(ObservedTitle({}));

// Update structure and component
structure.cursor('hero').set('name', 'Black Widow');

// Also for things like async fetch
fetch('./hero.json')
  .then(r => r.json())
  .then(d => structure.cursor().set('hero', Immutable.Map(d));