Application API

dispatcher

The application will create an instance of dispatcher when first created.

register(id, type)

Creates an instance of the given type, passing the id and application into the types constructor. The instance will then be accessible on the application object with the given Id.

function Foo(options) {
  console.log(options.id, options.app); // foo, Application(...)
}

var app = new Marty.Application();

app.register('foo', Foo);

register(registrations)

Same as register except you pass in an object literal where the keys are the Ids and the value is the type. If the value is an object literal when the instance will be accessible on the app within a child object.

var app = new Marty.Application();

app.register({
  foo: Foo,
  bar: {
    baz: Baz
  }
});

console.log(app.bar.baz);

replaceState(stores)

Replaces (Store#replaceState) the state of all stores with the values passed in. The key within the stores object literal must be the Id of the store.

var app = new Marty.Application();

app.register('fooStore', require('./stores/fooStore'));
app.register('barStore', require('./stores/barStore'));

app.replaceState({
  fooStore: {
    state: {
      [123]: { id: 123, name: 'Foo' }
    }
  },
  barStore: {
    state: {
      [456]: { id: 456, name: 'Foo' }
    }
  }
});

app.fooStore.getFoo(123) // { id: 123, name: 'Foo' }

clearState()

Calls Store#clear on all registered stores.

dehydrate()

Calls Store#dehydrate if present or Store#getState on all registered stores. Returning all states as a serializable object literal where they key is the Id of the store.

rehydrate([states])

Given some dehyrdated state, it will call Store#rehydrate if present or Store#replaceState on all registered stores passing in the dehyrdated state. The key of the states must match the Id of the store. If you don't pass in states then it will look at the window.__marty.state.

renderToString(Component, options)

Renders the given element to a string, waits for all fetches to complete and then re-renders element. Returns a promise which resolves once element is re-rendered. Result of render is an object containing the html body and the state as a script tag. timeout allows you to configure how long to wait for a fetch to finish before re-rendering the component (Default 1000ms).

var app = new Application();
var User = require('./views/user');

app.renderToString(<User id={123} />, { timeout: 2000}).then(function (res) {
  console.log('HTML body', res.htmlBody);
  console.log('HTML state', res.htmlState);
  console.log('Diagnostics', res.diagnostics);
});

renderToStaticMarkup(Component, options)

Same as renderToString except using React.renderToStaticMarkup.

getAll(type)

Get all instances of the given type. Result is an object literal where the keys is the instance id and the value is the instance.

app.register({
  fooStore: FooStore,
  barStore: { store: BarStore }
})

app.getAll('Store') // => { 'fooStore': ..., 'bar.store': ...  }