Skip to content

Instantly share code, notes, and snippets.

@jcreamer898
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcreamer898/8984870 to your computer and use it in GitHub Desktop.
Save jcreamer898/8984870 to your computer and use it in GitHub Desktop.
// Before switch
_.each(conditions.conditions, function (condition) {
field = this.lastRecord[condition.field];
var isVisible = false;
switch(condition.operator) {
case "+":
isVisible = (x == y);
break;
case "!=":
isVisisble = (x !== y);
break
// Lots more
// of this
// monkey
// business
}
}, this);
// After switch
_.each(conditions.conditions, function (condition) {
field = this.lastRecord[condition.field];
isVisible = operators[condition.operator](field, condition.value);
}, this);
// Operators.js
// A separate reusable file of operators that's nicer looking IMHO
define({
'=': function(x, y) {
return x == y;
},
'!=': function(x, y) {
return x !== y;
},
'>': function(x, y) {
return +x > +y;
},
'<': function(x, y) {
return +x < +y;
},
'defined': function(x) {
return x;
},
'undefined': function(x) {
return !x;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment