Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State Mixin for Stores #290

Closed
jehoshua02 opened this issue Mar 14, 2015 · 3 comments
Closed

State Mixin for Stores #290

jehoshua02 opened this issue Mar 14, 2015 · 3 comments
Milestone

Comments

@jehoshua02
Copy link

This is what I'm doing for state in stores:

var update = require('react/addons').addons.update;


var State = {
  init: function () {
    this.state = this.getInitialState();
  },

  setState: function (state) {
    this.state = update(this.state, {$merge: state});
    this.trigger(state);
  }
};


module.exports = State;

Then I use it like so in my Store:

var Reflux = require('reflux');
var Actions = require('../actions');
var State = require('./mixins/State');
var firebase = require('../util/firebase').child('requirements');


var RequirementStore = Reflux.createStore({
  mixins: [State],

  getInitialState: function () {
    return {
      requirements: []
    };
  },

  listenables: {
    save: Actions.REQUIREMENT_SAVE,
    sync: Actions.REQUIREMENT_SYNC
  },

  init: function () {
    firebase.on('value', Actions.REQUIREMENT_SYNC);
  },

  onSave: function (requirement) {
    firebase.push(requirement);
    this.setState({
      requirements: this.state.requirements.concat(requirement)
    });
  },

  onSync: function (snapshot) {
    var requirements = snapshot.val();
    if (!requirements) { return; }
    this.setState({
      requirements: requirements
    });
  }
});


module.exports = RequirementStore;

But I'm totally new. So maybe I don't understand the architectural concerns or missed some state mixin already in Reflux.

If this is a good feature maybe absorb it into Reflux?

@zkilgore
Copy link

I don't have an opinion on baking this into the library, but I will say I really like this pattern. Am going to put it into practice in some of my stores today. Nice idea and thanks for sharing!

@spoike
Copy link
Member

spoike commented Apr 7, 2015

I'm starting to lean towards adding a state to stores, though my requirements would be to have a way to do it with an immutable map. The best way would be to use a lib for immutable data structures, though this goes against having the library as dependency free as possible.

So for now, you can mixin this for yourself... or if anyone is kind enough, provide an npm/bower module for this.

@yonatanmn
Copy link
Contributor

npm (with additional way of use)

@spoike spoike added this to the 0.2.10 milestone Jul 18, 2015
@spoike spoike closed this as completed in 2d4725b Jul 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants