I have 3 filters I need to write for a project, all 3 of them do the same thing, but with a different array from the $rootScope (They are translating a numeric status into its display value).
How can I write this code once and call it from all 3 filters AND keep that one instance inside the myFilters module?
I'm not seeing a place I can put that code. If I was writing a service, I'd have a block I could put it in, but with filters, I don't have that common spot to write the code.
Right now I've just moved it to global scope, but I'd like to move it into the filter module so I don't have to repeat it 3 times, or store it on the global scope.
angular.module('myFilters', [])
.filter('aStatus', function($rootScope) {
return function(id, attr) {
var objectArray = $rootScope.appConfig.aStatuses,
index = _.findIndex(objectArray, function(obj) { return obj.id === id; });
if(index < 0) {
return "";
}
if(attr) {
return objectArray[index][attr];
}
return objectArray[index];
};
})
.filter('bStatus', function($rootScope) {
return function(id, attr) {
var objectArray = $rootScope.appConfig.aStatuses,
index = _.findIndex(objectArray, function(obj) { return obj.id === id; });
if(index < 0) {
return "";
}
if(attr) {
return objectArray[index][attr];
}
return objectArray[index];
};
})
.filter('cStatus', function($rootScope) {
return function(id, attr) {
var objectArray = $rootScope.appConfig.cStatuses,
index = _.findIndex(objectArray, function(obj) { return obj.id === id; });
if(index < 0) {
return "";
}
if(attr) {
return objectArray[index][attr];
}
return objectArray[index];
};
});