Browse files

adds where wrapper method

  • Loading branch information...
1 parent f994143 commit a73336fc5ca8b1f313c405d4987380c26f65c06d @cezary cezary committed Feb 10, 2012
Showing with 40 additions and 34 deletions.
  1. +19 −30 js/backbone-query.js
  2. +5 −1 src/backbone-query.coffee
  3. +5 −3 test/backbone-query-test.coffee
  4. +11 −0 test/backbone-query-test.js
View
49 js/backbone-query.js
@@ -1,14 +1,17 @@
-
-/*
-Backbone Query - A lightweight query API for Backbone Collections
-(c)2012 - Dave Tonge
-May be freely distributed according to MIT license.
-*/
-
(function() {
- var get_cache, get_models, get_sorted_models, iterator, page_models, parse_query, perform_query, process_query, sort_models, test_model_attribute, test_query_value,
- __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
-
+ /*
+ Backbone Query - A lightweight query API for Backbone Collections
+ (c)2012 - Dave Tonge
+ May be freely distributed according to MIT license.
+ */
+
+ var get_cache, get_models, get_sorted_models, iterator, page_models, parse_query, perform_query, process_query, sort_models, test_model_attribute, test_query_value;
+ var __indexOf = Array.prototype.indexOf || function(item) {
+ for (var i = 0, l = this.length; i < l; i++) {
+ if (this[i] === item) return i;
+ }
+ return -1;
+ };
parse_query = function(raw_query) {
var key, o, query_param, type, value, _results;
_results = [];
@@ -36,7 +39,6 @@ May be freely distributed according to MIT license.
}
return _results;
};
-
test_query_value = function(type, value) {
switch (type) {
case "$in":
@@ -59,7 +61,6 @@ May be freely distributed according to MIT license.
return true;
}
};
-
test_model_attribute = function(type, value) {
switch (type) {
case "$like":
@@ -79,7 +80,6 @@ May be freely distributed according to MIT license.
return true;
}
};
-
perform_query = function(type, value, attr, model) {
switch (type) {
case "$equal":
@@ -127,7 +127,6 @@ May be freely distributed according to MIT license.
return false;
}
};
-
iterator = function(models, query, andOr, filterReject) {
var parsed_query;
parsed_query = parse_query(query);
@@ -143,7 +142,6 @@ May be freely distributed according to MIT license.
return !andOr;
});
};
-
process_query = {
$and: function(models, query) {
return iterator(models, query, false, "filter");
@@ -158,7 +156,6 @@ May be freely distributed according to MIT license.
return iterator(models, query, false, "reject");
}
};
-
get_cache = function(collection, query, options) {
var cache, models, query_string, _ref;
query_string = JSON.stringify(query);
@@ -170,7 +167,6 @@ May be freely distributed according to MIT license.
}
return models;
};
-
get_models = function(collection, query) {
var compound_query, models, reduce_iterator;
compound_query = _.intersection(["$and", "$not", "$or", "$nor"], _(query).keys());
@@ -184,14 +180,12 @@ May be freely distributed according to MIT license.
return _.reduce(compound_query, reduce_iterator, models);
}
};
-
get_sorted_models = function(collection, query, options) {
var models;
models = get_models(collection, query);
if (options.sortBy) models = sort_models(models, options);
return models;
};
-
sort_models = function(models, options) {
if (_(options.sortBy).isString()) {
models = _(models).sortBy(function(model) {
@@ -203,7 +197,6 @@ May be freely distributed according to MIT license.
if (options.order === "desc") models = models.reverse();
return models;
};
-
page_models = function(models, options) {
var end, sliced_models, start, total_pages;
if (options.offset) {
@@ -221,14 +214,10 @@ May be freely distributed according to MIT license.
}
return sliced_models;
};
-
if (typeof require !== 'undefined') {
if (typeof _ === "undefined" || _ === null) _ = require('underscore');
- if (typeof Backbone === "undefined" || Backbone === null) {
- Backbone = require('backbone');
- }
+ if (typeof Backbone === "undefined" || Backbone === null) Backbone = require('backbone');
}
-
Backbone.QueryCollection = Backbone.Collection.extend({
query: function(query, options) {
var models;
@@ -241,13 +230,13 @@ May be freely distributed according to MIT license.
if (options.limit) models = page_models(models, options);
return models;
},
+ where: function(params, options) {
+ if (options == null) options = {};
+ return new this.constructor(this.query(params, options));
+ },
reset_query_cache: function() {
return this._query_cache = {};
}
});
-
- if (typeof exports !== "undefined") {
- exports.QueryCollection = Backbone.QueryCollection;
- }
-
+ if (typeof exports !== "undefined") exports.QueryCollection = Backbone.QueryCollection;
}).call(this);
View
6 src/backbone-query.coffee
@@ -200,10 +200,14 @@ Backbone.QueryCollection = Backbone.Collection.extend
# Return the results
models
+ # Where method wraps query and returns a new collection
+ where: (params, options = {})->
+ new @constructor @query params, options
+
# Helper method to reset the query cache
# Defined as a separate method to make it easy to bind to collection's change/add/remove events
reset_query_cache: -> @_query_cache = {}
# On the server the new Query Collection is added to exports
unless typeof exports is "undefined"
- exports.QueryCollection = Backbone.QueryCollection
+ exports.QueryCollection = Backbone.QueryCollection
View
8 test/backbone-query-test.coffee
@@ -329,6 +329,8 @@ test "null attribute with various operators", ->
result = a.query wrong_key: {$nin: [12,23]}
equal result.length, 0
-
-
-
+test "Where method", ->
+ a = create()
+ result = a.where likes: $gt: 5
+ equal result.length, 2
+ equal result.models.length, result.length
View
11 test/backbone-query-test.js
@@ -725,4 +725,15 @@
});
return equal(result.length, 0);
});
+ test("Where method", function() {
+ var a, result;
+ a = create();
+ result = a.where({
+ likes: {
+ $gt: 5
+ }
+ });
+ equal(result.length, 2);
+ return equal(result.models.length, result.length);
+ });
}).call(this);

0 comments on commit a73336f

Please sign in to comment.