mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
loadsh to simplify store
This commit is contained in:
parent
860a998623
commit
acf7445790
@ -46,6 +46,7 @@
|
||||
"jsx-loader": "0.12.2",
|
||||
"jsxhint": "0.4.15",
|
||||
"knex": "^0.7.3",
|
||||
"lodash": "^3.5.0",
|
||||
"log4js": "0.6.21",
|
||||
"moment": "^2.9.0",
|
||||
"nconf": "0.6.9",
|
||||
|
@ -2,12 +2,18 @@ var Reflux = require('reflux');
|
||||
var FeatureActions = require('./FeatureToggleActions');
|
||||
var Server = require('./FeatureToggleServerFacade');
|
||||
var Timer = require('../utils/Timer');
|
||||
var filter = require('lodash/collection/filter');
|
||||
var sortBy = require('lodash/collection/sortBy');
|
||||
var findIndex = require('lodash/array/findIndex');
|
||||
|
||||
//TODO: have archived toggles in seperate store.
|
||||
var _featureToggles = [];
|
||||
var _archivedToggles = [];
|
||||
|
||||
// Creates a DataStore
|
||||
var FeatureStore = Reflux.createStore({
|
||||
//The store should be split in two: toggleStore && archivedToggleStore!
|
||||
|
||||
// Initial setup
|
||||
init: function() {
|
||||
this.listenTo(FeatureActions.create.completed, this.onCreate);
|
||||
@ -15,11 +21,13 @@ var FeatureStore = Reflux.createStore({
|
||||
this.listenTo(FeatureActions.archive.completed, this.onArchive);
|
||||
this.listenTo(FeatureActions.revive.completed, this.onRevive);
|
||||
|
||||
//TODO: this should not be part of the store!
|
||||
this.timer = new Timer(this.loadDataFromServer, 30*1000);
|
||||
this.timer.start();
|
||||
},
|
||||
|
||||
loadDataFromServer: function() {
|
||||
//TODO: this should not be part of the store!
|
||||
Server.getFeatures(function(err, featureToggles) {
|
||||
this.setToggles(featureToggles);
|
||||
}.bind(this));
|
||||
@ -35,30 +43,22 @@ var FeatureStore = Reflux.createStore({
|
||||
},
|
||||
|
||||
setToggles: function(toggles) {
|
||||
_featureToggles = toggles;
|
||||
_featureToggles = sortBy(toggles, 'name');
|
||||
this.trigger();
|
||||
},
|
||||
|
||||
onUpdate: function(feature) {
|
||||
var idx;
|
||||
_featureToggles.forEach(function(item, i) {
|
||||
if(item.name === feature.name) {
|
||||
idx = i;
|
||||
}
|
||||
});
|
||||
var idx = findIndex(_featureToggles, 'name', feature.name);
|
||||
_featureToggles[idx] = feature;
|
||||
this.trigger();
|
||||
},
|
||||
|
||||
onArchive: function(feature) {
|
||||
var idx;
|
||||
_featureToggles.forEach(function(item, i) {
|
||||
if(item.name === feature.name) {
|
||||
idx = i;
|
||||
}
|
||||
var featureToggles = filter(_featureToggles, function(item) {
|
||||
return item.name !== feature.name;
|
||||
});
|
||||
_featureToggles.splice(idx, 1);
|
||||
_archivedToggles.unshift(feature);
|
||||
this.setToggles(featureToggles);
|
||||
this.trigger();
|
||||
},
|
||||
|
||||
@ -67,7 +67,7 @@ var FeatureStore = Reflux.createStore({
|
||||
return f.name !== item.name;
|
||||
});
|
||||
_archivedToggles = newStore;
|
||||
_featureToggles.push(item);
|
||||
this.setToggles(_featureToggles.concat([item]));
|
||||
this.trigger();
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user