reflux / refluxjs

A simple library for uni-directional dataflow application architecture with React extensions inspired by Flux
BSD 3-Clause "New" or "Revised" License
5.36k stars 330 forks source link

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

Closed yonatanmn closed 8 years ago

yonatanmn commented 8 years ago

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>
        );
    }
});
devinivy commented 8 years ago

Hi @yonatanmn– given that this isn't a bug or feature request, do you mind moving this over to our new discussion repo (https://github.com/reflux/discuss)?

yonatanmn commented 8 years ago

Moved, though I wasn't aware of this new Repo, and I guess it's the same for many others... here

devinivy commented 8 years ago

Thanks :) It's brand new. I'll try to stop by all the reflux chatrooms to let folks know.