How can I test for an empty Breeze predicate?

Posted by Megan on Stack Overflow See other posts from Stack Overflow or by Megan
Published on 2013-06-26T15:19:50Z Indexed on 2013/06/26 16:21 UTC
Read the original article Hit count: 342

Filed under:

I'm using Breeze to filter data requested on the client. My code looks a little like this:

Client - Creating Filter Predicate

var predicates = [];
var criteriaPredicate = null;
$.each(selectedFilterCriteria(), function (index, item) {
    criteriaPredicate = (index == 0)
        ? breeze.Predicate.create('criteriaId', breeze.FilterQueryOp.Equals, item)
        : criteriaPredicate.or('criteriaId', breeze.FilterQueryOp.Equals, item);
if (breeze.Predicate.isPredicate(criteriaPredicate)) {
    predicates.push(criteriaPredicate);
}

// Repeat for X Filter Criteria

var filter = breeze.Predicate.and(predicates);
return context.getAll(filter, data);

Client - Context Query

function getAll(predicate, dataObservable) {
    var query = breeze.EntityQuery.from('Data');
    if (breeze.Predicate.isPredicate(predicate)) {
        query = query.where(predicate);
    }
    return manager.executeQuery(query).then(success).fail(failure);
}

Issue

I'm having an issue with the request because, if there are no filters set, I apply an "empty" predicate (due to the var filter = breeze.Predicate.and([]) line) resulting in a request like http://mysite/api/app/Data?$filter=. The request is an invalid OData query since the value of the $filter argument cannot be empty.

Is there a good way for me to check for an empty predicate? I know I can refactor my client code to not use a predicate unless there is at least one filterable item, but I thought I would check first to see if I overlooked some property or method on the Breeze Predicate.

© Stack Overflow or respective owner

Related posts about breeze