I'm unsure about how to simplify this method properly. The method is basically responsible to allocate data.
The response object is a collection of objects. Each object contains an attribute called order
and i have to distinguish between visible and invisible elements. Visible elements are represented by positive integers, invisible elements by a negative integers.
Also it is possible to overwrite the default state if the URL contains an parameter (hidden) with a list of hidden element ids. ?hidden=23,432,3,123
_hasParam()
returns boolean
_getParam()
returns string
(separated by comma [23,432,3,123])
contains()
returns boolean
prepare: function (response) {
var visible = [];
var invisible = [];
if (_hasParam('hidden')) {
var hidden = _getParam('hidden').split(',');
angular.forEach(response, function (item) {
if (hidden.contains(item.id)) {
invisible.push(item);
} else {
visible.push(item);
}
});
} else {
angular.forEach(response, function (item) {
if (item.order === -1) {
invisible.push(item);
} else {
visible.push(item);
}
});
}
$rootScope.visible = visible;
$rootScope.invisible = invisible;
}