Top-Level API

Registration

Marty.register(class, [id])

Register a Marty class into the registry. Instantiates an instance with the dispatcher and returns it. Optionally takes a store Id as the second argument. Defaults to the store name.

class UserStore extends Marty.Store {
  ...
}

module.exports = Marty.register(UserStore, 'users');

Marty.createStore(options)

Register a store defined with the ES5 syntact into the registry. Instantiates an instance with the dispatcher and returns it.

Marty.createQueries(options)

Register queries defined with the ES5 syntact into the registry. Instantiates an instance with the dispatcher and returns it.

Marty.createStateSource(options)

Register a state source defined with the ES5 syntact into the registry. Instantiates an instance with the dispatcher and returns it.

Marty.createActionCreators(options)

Register action creators defined with the ES5 syntact into the registry. Instantiates an instance with the dispatcher and returns it.

Marty.createStateMixin(options)

Creates a state mixin and returns it.

Marty.createConstants(options)

Creates constants and returns them.

Marty.createContainer(InnerComponent, options)

Wraps the component with a container component that is responsible for fetching state from stores and passing it to the inner component.

Marty.registry

Returns the current instance of the registry.

State

Marty.replaceState(stores, [context])

Replaces (Store#replaceState) the state of all stores with the values passed in. If a context is passed in then we will replace the context's stores state. The key within the stores object literal must be the Id of the store.

Marty.replaceState({
  UserStore: {
    state: {
      [123]: { id: 123, name: 'Foo' }
    }
  }
});

UserStore.getUser(123) // { id: 123, name: 'Foo' }

Marty.clearState([context])

Calls Store#clear on all registered stores. If a context is passed in then we will call Store#clear) on all stores within the context.

Marty.dehydrate([context])

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. If a context is passed in then we will dehyrdate all stores within the context.

Marty.rehydrate([states], [context])

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. If a context is passed in then we will rehyrdate all stores within the context.

Marty.createContext([values])

Creates a context which contains a dispatcher and instances of all types currently registered within Marty.registry. Optionally will be extended with values object.

Marty.renderToString(options)

Renders the given component type with the given props to string, waits for all fetches to complete and then re-renders component. Returns a promise which resolves once component is re-rendered. Result of render is an object containing the rendered string and an object detailing what fetches occurred. timeout allows you to configure how long to wait for a fetch to finish before re-rendering the component (Default 1000ms). It uses React contexts to pass the Marty context to child components (context key is marty).

var options = {
  type: Foo,
  context: context,
  props: { bar: 'bar' },
  timeout: 1000
};

var Foo = React.createClass({
  contextTypes {
    marty: React.PropTypes.object
  },
  render: function () {
    var store = this.context.marty.getStore('foo');

    return <div />
  }
});

Marty.renderToString(options).then(function (res) {
  console.log('Rendered html', res.html);
  console.log('Diagnostics', res.diagnostics);
});

Environment

Marty.isServer

True if the current process is being executed within node.js or io.js.

Marty.isBrowser

True if the current process is being executed within a browser.

General

Marty.dispatcher

Returns the current instance of the dispatcher.

Marty.warnings

Configurable list of warnings that Marty emits. Setting the key to false will stop the warning from happening. We will warn you when a feature is being depreciated so disabling warnings can make upgrading difficult in the future.

Marty.warnings.invokeConstant = false;

Marty.warnings.without(warningsToDisable*, callback, [context])

Disables a warning for the duration of the callback.

Marty.warnings.without(['reservedFunction', 'superNotCalledWithOptions'], function () {
  // do something evil
});

Marty.createInstance()

Creates a new instance of Marty. Useful for testing.

Marty.dispose()

Disposes of all state and event listeners for all known instances.

Marty.version

The current version of Marty.