Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

On Models and Stores

Mitch VanDuyn edited this page Jan 18, 2017 · 4 revisions

Struggling with the difference between these concepts:

Here is the world's simplest flux store

// Single object representing list data and logic
var ListStore = {

    // Actual collection of model data
    items: []

};

// Tell the dispatcher we want to listen for *any*
// dispatched events

MicroEvent.mixin( ListStore );

AppDispatcher.register( function( payload ) {

    switch( payload.actionName ) {

        // Do we know how to handle this action?
        case 'new-item':

            // We get to mutate data!
            ListStore.items.push( payload.newItem );
            // Tell the world we changed!
            ListStore.trigger( 'change' );
            break;

    }

}); 

ListActions = {

    add: function( item ) {
        AppDispatcher.dispatch({
            eventName: 'new-item',
            newItem: item
        });
    }

};

With HyperStore we would might say:

class List < HyperStore::Base
  private_state list: [], scope: :class
  def add!(item)
    state.list! << item
  end
end

and with HyperMesh (ActiveRecord):

class List < ActiveRecord::Base
end

which would also persist the new items.

FYI there are libraries like Mobx and Reflux that implement flux without as much boilerplate, but none as short as Hyperloop, mainly because of Ruby's superior meta-programming capabilities that allows us to hide the boilerplate behind methods like private_state, and state in the base classes.

So here is my question: In several places statements are made like: A store is not a model. A store contains models.. Huh? Please somebody edit this document and explain it to me.

Clone this wiki locally