Skip to content

No Action files! another step in being super lazy. #1

@yonatanmn

Description

@yonatanmn

Normally in reflux we have several Actions files, containing list of strings, and in the stores we add methods like: onSomeAction. we have to manually connect everything, and when we change the name of the Action we do it twice or more.
Usually, I don't share Actions between stores, and this duplication is really annoying.
So I came up with this:

function createStoreAndActions(storeObj) {

    function startWithOn(str) {
        return str[0] === 'o' && str[1] === 'n' && str[2].toUpperCase() === str[2]
    }

    var ActionStrs = Object.keys(storeObj).filter(startWithOn);
    let Actions = reflux.createActions(ActionStrs);

    if (storeObj.listenables) {
        if (Array.isArray(storeObj.listenables)) {
            storeObj.listenables.push(Actions);
        } else {
            storeObj.listenables = [storeObj.listenables, Actions];
        }
    } else {
        storeObj.listenables = Actions;
    }

    return {
        Store: reflux.createStore(storeObj),
        Actions
    }

}

the convention I've used is taking all methods starting with 'on' and having capital letter after that. Of course it's easy to implement any other convention (e.g passing to create an array of strings etc)
then I can just specify those methods, and an auto actions will be created.

let {Store, Actions} = createStoreAndActions({
    mixins:[...],
     onTestAction(){
       console.log('it is working with no explicit Action!')
    }
});

export {Actions};
export default Store;

and in Component:

import TestStore, {Actions as testActions} from '../Stores/TestStore';
var Component = React.createClass({
    render: function() {
        return (
          <div onClick={testActions.onTestAction}>click here to log from store</div>
        );
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions