Store

Store

A Store is the basic state container, it provides a reference to the global dispatcher, the shared state
and a way to dispatch and reduce actions.

It represent one stand-alone source of true.

Remember: It must be added to flue by calling .addStore or .addStores

Constructor

new Store(config)

A name can be added to the store to make it referenceable from flue. E.g

flue.addStore(new Store({ name: 'myStore' }))
// then it can be called from vue 
flue.refs.myStore

At creation, the initial state can be passed to the Store directly.

Source:
Parameters:
Name Type Description
config Object

Configuration of the store

Methods

actions(context)

The store may expose a object of function that can be called in order to dispatch Actions.

Source:
Parameters:
Name Type Description
context Store

The store itself is passed as context

createAction(type, payload) → {Action}

This function can be called in order to create a Action instance without have to import Action from Flue

Source:
Parameters:
Name Type Description
type String

The type of the action

payload Object

The payload of the action

Returns:
Type:
Action

A newly created Action instance

dispatch(action)

This function is used to dispatch actions, it is a wrapper around _dispatcher.dispatch in order to provide more options to the client

Source:
Parameters:
Name Type Description
action Object

Action instance, or an Action-like vanilla object

dispatchAction(type, payload)

Explicity create and dispatch an Action type

Source:
Parameters:
Name Type Description
type String

The type of the action

payload Object

The payload of the action

getState() → {Object}

Get the current state

Source:
Returns:
Type:
Object

A copy of the current state

initialize()

Runtime initialization of the dispatcher and the vm. At any point a store can become self-sufficient by calling this function. The global dispatcher will be override with the stores' local one and a Vue vm will be spawned to handle the state reactivity

Source:

reduce(action)

The reduce function takes a action as input and reduce it by switching behavior based on its type

Source:
Parameters:
Name Type Description
action Object

A action instance

reduceMap(action, actionFunctionMap, payloadKey)

Map each action to the correct function.

Example:

this.reduceMap(action, {
 ADD_FOOD_TO_SHOPPING_CART: this.addFoodToShoppingCart,
 REMOVE_FOOD_FROM_SHOPPING_CART: this.removeFoodFromShoppingCart,
 CHECK_OUT: this.checkOut
})
Source:
Parameters:
Name Type Default Description
action Object

A action

actionFunctionMap Object

A dictionary with action type as key and function as value

payloadKey String payload

The key to access to the data part of an Action

subscribe(func) → {function}

This function let the user subscribe to the state changing. Example:

var unsubscribe = Store.subscribe(subscribeFunction)
unsubscribe()
Source:
Parameters:
Name Type Description
func function

A callback that will be called everytime the state is changed

Returns:
Type:
function

A unsubscribe function.