Skip to content
Simon Y. Blackwell edited this page Apr 27, 2015 · 3 revisions

#Adding Predicates

So you have a method on a class that returns true or false and you want to use it in JOQULAR? Easy! Just wrap your method as follows:

(MyClass.prototype.myPredicate = function(valueToCheck) { 
      if(...) { return true; } else { return false; }).predicate = true;

If your predicate takes multiple arguments, then things get a little trickier:

(MyClass.prototype.myPredicate = function(param1,param2) {
       if(param1 instanceof Array) {
           return this.myPredicate.apply(this,param1);
       }
       if(...) { return true; } else { return false; }).predicate = true;

Adding Providers

If you want to compare a value in a query to the result of a function call you need to configure the function as a provider. It is done the same way as predicates, except providers can't take arguments:

(MyClass.prototype.myProvider = function() { 
      if(this.x===this.y) { return this.z; } else { return null; }).provider = true;

Making It Even Easier

Adding all that array checking and recursion is a pain if you have 10 or 20 predicates to add, so use this function and you don't have to muck with the internals of your well tested functions:

function toPredicate(f) {
		var predicate = function() {
			if(arguments[0] instanceof Array && f.length>1) {
				return f.apply(this,arguments[0]);
			}
			return f.apply(this,arguments);
		};
		predicate.predicate = true;
		return predicate;
	}

MyClass.prototype.myPredicate = toPredicate(function(param1,param2) {
       if(param1 instanceof Array) {
           return this.myPredicate.apply(this,param1);
       }
       if(...) { return true; } else { return false; });
Clone this wiki locally