From acf74457901f8702ab32a46e4e13bf3572d60e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Tue, 17 Mar 2015 18:16:06 +0100 Subject: [PATCH] loadsh to simplify store --- package.json | 1 + public/js/stores/FeatureToggleStore.js | 28 +++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 8818dbd6ad..4bc092089a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/public/js/stores/FeatureToggleStore.js b/public/js/stores/FeatureToggleStore.js index e8567c0a74..d42c1baf97 100644 --- a/public/js/stores/FeatureToggleStore.js +++ b/public/js/stores/FeatureToggleStore.js @@ -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(); },