DRY principle for storage IDs
This commit is contained in:
1 parent
85dfcbd156
commit
7988e7487f
1 file changed
+14
-9
@@ -20,6 +20,11 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor
|
||||
self.currentFilters = ko.observableArray(self.defaultFilters);
|
||||
self.selectedItem = ko.observable(undefined);
|
||||
|
||||
self.storageIds = {
|
||||
"currentSorting": self.listType + "." + "currentSorting",
|
||||
"currentFilters": self.listType + "." + "currentFilters",
|
||||
};
|
||||
|
||||
//~~ item handling
|
||||
|
||||
self.refresh = function() {
|
||||
@@ -281,16 +286,16 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor
|
||||
if ( self._initializeLocalStorage() ) {
|
||||
var currentSorting = self.currentSorting();
|
||||
if (currentSorting !== undefined)
|
||||
localStorage[self.listType + "." + "currentSorting"] = currentSorting;
|
||||
localStorage[self.storageIds.currentSorting] = currentSorting;
|
||||
else
|
||||
localStorage[self.listType + "." + "currentSorting"] = undefined;
|
||||
localStorage[self.storageIds.currentSorting] = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
self._loadCurrentSortingFromLocalStorage = function() {
|
||||
if ( self._initializeLocalStorage() ) {
|
||||
if (_.contains(_.keys(supportedSorting), localStorage[self.listType + "." + "currentSorting"]))
|
||||
self.currentSorting(localStorage[self.listType + "." + "currentSorting"]);
|
||||
if (_.contains(_.keys(supportedSorting), localStorage[self.storageIds.currentSorting]))
|
||||
self.currentSorting(localStorage[self.storageIds.currentSorting]);
|
||||
else
|
||||
self.currentSorting(defaultSorting);
|
||||
}
|
||||
@@ -299,13 +304,13 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor
|
||||
self._saveCurrentFiltersToLocalStorage = function() {
|
||||
if ( self._initializeLocalStorage() ) {
|
||||
var filters = _.intersection(_.keys(self.supportedFilters), self.currentFilters());
|
||||
localStorage[self.listType + "." + "currentFilters"] = JSON.stringify(filters);
|
||||
localStorage[self.storageIds.currentFilters] = JSON.stringify(filters);
|
||||
}
|
||||
};
|
||||
|
||||
self._loadCurrentFiltersFromLocalStorage = function() {
|
||||
if ( self._initializeLocalStorage() ) {
|
||||
self.currentFilters(_.intersection(_.keys(self.supportedFilters), JSON.parse(localStorage[self.listType + "." + "currentFilters"])));
|
||||
self.currentFilters(_.intersection(_.keys(self.supportedFilters), JSON.parse(localStorage[self.storageIds.currentFilters])));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -313,11 +318,11 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor
|
||||
if (!Modernizr.localstorage)
|
||||
return false;
|
||||
|
||||
if (localStorage[self.listType + "." + "currentSorting"] !== undefined && localStorage[self.listType + "." + "currentFilters"] !== undefined && JSON.parse(localStorage[self.listType + "." + "currentFilters"]) instanceof Array)
|
||||
if (localStorage[self.storageIds.currentSorting] !== undefined && localStorage[self.storageIds.currentFilters] !== undefined && JSON.parse(localStorage[self.storageIds.currentFilters]) instanceof Array)
|
||||
return true;
|
||||
|
||||
localStorage[self.listType + "." + "currentSorting"] = self.defaultSorting;
|
||||
localStorage[self.listType + "." + "currentFilters"] = JSON.stringify(self.defaultFilters);
|
||||
localStorage[self.storageIds.currentSorting] = self.defaultSorting;
|
||||
localStorage[self.storageIds.currentFilters] = JSON.stringify(self.defaultFilters);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user