0

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];
    };
});
1
  • why dont you want to create only one filter and pass an array you want into it as a parameter? Commented Feb 7, 2014 at 4:54

1 Answer 1

1

Why not to extract generic function into service and just call it from filter with prebound objectArray?

angular.module('myFilters', [])
.factory("StatusFilter", function ($rootScope) {
    return function(objectArray, id, attr) {
        var index = _.findIndex(objectArray, function(obj) { return obj.id === id; });
        if(index < 0) {
            return "";
        }
        if(attr) {
            return objectArray[index][attr];
        }
        return objectArray[index];
    };
});
.filter('aStatus', function(StatusFilter, $rootScope) {
    return function(id, attr) {
        return StatusFilter($rootScope.appConfig.aStatuses, id, attr);
    };
})
.filter('bStatus', function(StatusFilter, $rootScope) {
    return function(id, attr) {
        return StatusFilter($rootScope.appConfig.bStatuses, id, attr);
    };
})
.filter('cStatus', function(StatusFilter, $rootScope) {
    return function(id, attr) {
        return StatusFilter($rootScope.appConfig.cStatuses, id, attr);
    };
})

Alternatively, you can extract that function into filter:

angular.module('myFilters', [])
.filter("Status", function ($rootScope) {
    return function(objectArray, id, attr) {
        var index = _.findIndex(objectArray, function(obj) { return obj.id === id; });
        if(index < 0) {
            return "";
        }
        if(attr) {
            return objectArray[index][attr];
        }
        return objectArray[index];
    };
});
.filter('aStatus', function($filter, $rootScope) {
    return function(id, attr) {
        return $filter("Status")($rootScope.appConfig.aStatuses, id, attr);
    };
})
.filter('bStatus', function($filter, $rootScope) {
    return function(id, attr) {
        return $filter("Status")($rootScope.appConfig.bStatuses, id, attr);
    };
})
.filter('cStatus', function($filter, $rootScope) {
    return function(id, attr) {
        return $filter("Status")($rootScope.appConfig.cStatuses, id, attr);
    };
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.