Skip to content

Latest commit

 

History

History
89 lines (53 loc) · 2.38 KB

API.md

File metadata and controls

89 lines (53 loc) · 2.38 KB

API

Disptacher

Phrontend uses facebook's dispatcher and follows the concept of a single dispatcher. All actions are dispatched via this dispatcher.

The Dispatcher exposed from Phrontend is a singleton.

import {Dispatcher} from 'phrontend';

Methods

.dispatch(actionType : <String>, data : <Any>) : void

  • dispatches an action of type actionType with the payload data

For all other methods, refer Facebook's dispatcher docs.

Store

import {Store} from 'phrontend';

Store is an abstract class. Your application Store extends this base Store and defines a method handler with the following signature

handler(payload : <Object>{ actionType : <String>, data : <Any> }) : void

This handler is registered to the AppDispatcher, and will be called whenever the dispatcher receives an action.

Example

class MyStore extends Store {
  handler(payload) {}
}

Methods

get( key : <String | Number | void> ) : Any

  • get() returns entire state object
  • get('foo') returns state['foo']
  • get(5) returns state[5] - used for Lists

set( key : <Object | String | Number>, value : <Any> ) : void

  • set('foo', 'bar') does state['foo'] = 'bar'
  • set({ foo: 'bar' }) does the same thing. Use this for setting multiple items together

parse( data : <Object | String>) : <Object>

  • parses only String using JSON.parse
  • Override in your Store definition to parse API responses in a custom manner

subscribe( successHandler : <Function>, errorHandler : <Function> ) : void

  • Listen to change event using successHandler
  • Listen to error event using errorHandler

unsubscribe( successHandler : <Function>, errorHandler : <Function> ) : void

  • Unlisten successHandler from the change event
  • Unlisten errorHandler from the error event

emitChange( data : <Any> )

  • emit a change event to all subscribers of the store instance

emitError( err : <Error> )

  • emit an error event to all subscribers of the store instance

dispatcher

  • Reference to the AppDispatcher's instance

dispatchToken

  • The store's dispatchToken. Use this to inform dispatcher.waitFor in other Stores

ApiCaller

ApiCaller is a singleton that uses whatwg-fetch