Permalink
Browse files

Fixed "$any" operator

  • Loading branch information...
1 parent 55c6c29 commit 2e1637d75a0ff268ac372ce5d1f1cc4250db5a38 @davidgtonge committed Mar 28, 2012
Showing with 62 additions and 6 deletions.
  1. +9 −4 js/backbone-query.js
  2. +1 −1 js/backbone-query.min.js
  3. +1 −1 src/backbone-query.coffee
  4. +16 −0 test/backbone-query-test.coffee
  5. +35 −0 test/backbone-query-test.js
View
@@ -27,6 +27,7 @@ May be freely distributed according to MIT license.
o.type = type;
switch (type) {
case "$elemMatch":
+ case "$relationMatch":
o.value = parse_query(value);
break;
case "$computed":
@@ -88,6 +89,8 @@ May be freely distributed according to MIT license.
case "$in":
case "$nin":
return value != null;
+ case "$relationMatch":
+ return (value != null) && value.models;
default:
return true;
}
@@ -123,8 +126,8 @@ May be freely distributed according to MIT license.
case "$nin":
return __indexOf.call(value, attr) < 0;
case "$all":
- return _(attr).all(function(item) {
- return __indexOf.call(value, item) >= 0;
+ return _(value).all(function(item) {
+ return __indexOf.call(attr, item) >= 0;
});
case "$any":
return _(attr).any(function(item) {
@@ -145,18 +148,20 @@ May be freely distributed according to MIT license.
return value.call(model, attr);
case "$elemMatch":
return iterator(attr, value, false, detect, "elemMatch");
+ case "$relationMatch":
+ return iterator(attr.models, value, false, detect, "relationMatch");
case "$computed":
return iterator([model], value, false, detect, "computed");
default:
return false;
}
};
- iterator = function(models, query, andOr, filterReject, subQuery) {
+ iterator = function(models, query, andOr, filterFunction, subQuery) {
var parsed_query;
if (subQuery == null) subQuery = false;
parsed_query = subQuery ? query : parse_query(query);
- return filterReject(models, function(model) {
+ return filterFunction(models, function(model) {
var attr, q, test, _i, _len;
for (_i = 0, _len = parsed_query.length; _i < _len; _i++) {
q = parsed_query[_i];
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -74,7 +74,7 @@ perform_query = (type, value, attr, model, key) ->
when "$between" then value[0] < attr < value[1]
when "$in" then attr in value
when "$nin" then attr not in value
- when "$all" then _(attr).all (item) -> item in value
+ when "$all" then _(value).all (item) -> item in attr
when "$any" then _(attr).any (item) -> item in value
when "$size" then attr.length is value
when "$exists", "$has" then attr? is value
@@ -436,6 +436,22 @@ test "$elemMatch", ->
#equal result[0].get("foo")[0].color, "purple"
#equal result[0].get("foo")[0].thick, false
+test "$any and $all", ->
+ a = name: "test", tags1: ["red","yellow"], tags2: ["orange", "green", "red", "blue"]
+ b = name: "test1", tags1: ["purple","blue"], tags2: ["orange", "red", "blue"]
+ c = name: "test2", tags1: ["black","yellow"], tags2: ["green", "orange", "blue"]
+ d = name: "test3", tags1: ["red","yellow","blue"], tags2: ["green"]
+ e = new QueryCollection [a,b,c,d]
+
+ result = e.query
+ tags1: $any: ["red","purple"] # should match a, b, d
+ tags2: $all: ["orange","green"] # should match a, c
+
+ equal result.length, 1
+ equal result[0].get("name"), "test"
+
+
+

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 2e1637d

Please sign in to comment.