Search code examples
javascriptreactjsrefluxjs

How do I make my store private in React reflux


How do I enforce my store functions and model private. If my store is

var employeeActions = Reflux.createActions(['addEmployee']);

var empStore = Reflux.createstore({
listenables: [employeeActions],
model: {
          Total:0,
          employees:[]
}
onAddEmployee: function(employee){
   this.model.employees.push(employee);
}
});

Even though flux says Actions->Store. The current object structure doesnt stop a developer in the team from calling empStore.OnAddEmployee ?


Solution

  • Your store should be in a module, assuming you are using browserify or webpack or something similar and you require your modules. So any variable / object / function you declare in the module but don't include in the object sent to the Reflux.createStore() is private.

    So something like this:

    var employeeActions = Reflux.createActions(['addEmployee']);
    
    var model: {
              Total:0,
              employees:[]
    }
    
    var empStore = Reflux.createstore({
    listenables: [employeeActions],
    
    onAddEmployee: function(employee){
       model.employees.push(employee);
    }
    });

    I would suggest keeping the onAddEmployee function on the object and rely on reflux to wire everything up if you're using the listenables mixin. Otherwise, just follow the examples on github: https://github.com/reflux/refluxjs#creating-data-stores and using the same principle, keep the function private, outside the object you are passing to the factory method.