patternjavascriptMinor
An object exposed to directives that need to track changes on it
Viewed 0 times
exposedtrackneedthatchangesobjectdirectives
Problem
This is an object exposed to the view and is being used on directives within the same view. The directives need to track changes on the
Other properties and methods are just being used on the view itself.
Filters object:
Directive example:
ctrl.filters.applied object. It's a simple part of a project at my work and I'd like to know if I'm doing it right.Other properties and methods are just being used on the view itself.
ctrl is the controller, since I'm using controllerAs syntax.var ctrl = this;Filters object:
ctrl.filters = (function filters() {
var currentFilters,
appliedFilters,
initialFilters = {
companies: [],
user: null,
workDate: {
initial: null,
final: null
}
};
currentFilters = angular.copy(initialFilters);
appliedFilters = angular.copy(currentFilters);
function apply() {
appliedFilters = angular.copy(currentFilters);
};
function clear() {
currentFilters = angular.copy(initialFilters);
appliedFilters = angular.copy(currentFilters);
};
return function filtersReturn() {
return {
current: currentFilters,
applied: appliedFilters,
apply: apply,
clear: clear
};
};
})();
Directive example:
Solution
return function filtersReturn() {
return {
current: currentFilters,
applied: appliedFilters,
apply: apply,
clear: clear
};
};This piece of code looks odd to me, could you simply return the object itself, like this:
var returnFilters = {
current: currentFilters,
applied: appliedFilters,
apply: apply,
clear: clear
;}
return returnFilters;I just don't see the point of returning a function that doesn't do "function" stuff, it really returns a function that returns an object, so why not just return the object to begin with?
One more thing, I didn't like the name of the function, so when I changed it to an object I changed the naming as well. The name
returnFilters sounds better than filtersReturn I mean when I say "filters return" I am thinking "filters return ... what?" but these are the return filters so I want the name to express that for me so when I read the code later I don't get confused as to what this filter is doing.Code Snippets
return function filtersReturn() {
return {
current: currentFilters,
applied: appliedFilters,
apply: apply,
clear: clear
};
};var returnFilters = {
current: currentFilters,
applied: appliedFilters,
apply: apply,
clear: clear
;}
return returnFilters;Context
StackExchange Code Review Q#114467, answer score: 2
Revisions (0)
No revisions yet.