Skip to content

Commit

Permalink
Updated documentation and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgtonge committed Feb 2, 2012
1 parent 3189b91 commit a255079
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions js/backbone-query.js
Expand Up @@ -6,7 +6,7 @@ May be freely distributed according to MIT license.
*/ */


(function() { (function() {
var array_intersection, get_cache, get_models, get_sorted_models, iterator, page_models, parse_query, process_query, sort_models, test_attr, test_query_value, var array_intersection, get_cache, get_models, get_sorted_models, iterator, page_models, parse_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; }; __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; };


array_intersection = function(arrays) { array_intersection = function(arrays) {
Expand Down Expand Up @@ -69,7 +69,7 @@ May be freely distributed according to MIT license.
} }
}; };


test_attr = function(type, value) { test_model_attribute = function(type, value) {
switch (type) { switch (type) {
case "$like": case "$like":
case "$regex": case "$regex":
Expand All @@ -96,7 +96,7 @@ May be freely distributed according to MIT license.
for (_i = 0, _len = parsed_query.length; _i < _len; _i++) { for (_i = 0, _len = parsed_query.length; _i < _len; _i++) {
q = parsed_query[_i]; q = parsed_query[_i];
attr = model.get(q.key); attr = model.get(q.key);
test = test_attr(q.type, attr); test = test_model_attribute(q.type, attr);
if (test) { if (test) {
test = ((function() { test = ((function() {
var _ref; var _ref;
Expand Down
2 changes: 1 addition & 1 deletion js/backbone-query.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/backbone-query.coffee
Expand Up @@ -48,7 +48,8 @@ test_query_value = (type, value) ->
when "$cb" then _(value).isFunction() when "$cb" then _(value).isFunction()
else true else true


test_attr = (type, value) -> # Test each attribute that is being tested to ensure that is of the correct type
test_model_attribute = (type, value) ->
switch type switch type
when "$like", "$regex" then _(value).isString() when "$like", "$regex" then _(value).isString()
when "$contains", "$all", "$any" then _(value).isArray() when "$contains", "$all", "$any" then _(value).isArray()
Expand All @@ -66,7 +67,8 @@ iterator = (collection, query, andOr, filterReject) ->
# Retrieve the attribute value from the model # Retrieve the attribute value from the model
attr = model.get(q.key) attr = model.get(q.key)
# Check if the attribute value is the right type (some operators need a string, or an array) # Check if the attribute value is the right type (some operators need a string, or an array)
test = test_attr(q.type, attr) test = test_model_attribute(q.type, attr)
# If the attribute test is true, perform the query
if test then test = (switch q.type if test then test = (switch q.type
when "$equal" then attr is q.value when "$equal" then attr is q.value
when "$contains" then q.value in attr when "$contains" then q.value in attr
Expand Down Expand Up @@ -94,14 +96,17 @@ iterator = (collection, query, andOr, filterReject) ->
# For an "and" query, if all the queries are true, then we return true # For an "and" query, if all the queries are true, then we return true
not andOr not andOr



# An object with or, and, nor and not methods
# A object with or, and, nor and not methods
process_query = process_query =
$and: (collection, query) -> iterator collection, query, false, "filter" $and: (collection, query) -> iterator collection, query, false, "filter"
$or: (collection, query) -> iterator collection, query, true, "filter" $or: (collection, query) -> iterator collection, query, true, "filter"
$nor: (collection, query) -> iterator collection, query, true, "reject" $nor: (collection, query) -> iterator collection, query, true, "reject"
$not: (collection, query) -> iterator collection, query, false, "reject" $not: (collection, query) -> iterator collection, query, false, "reject"



# This method attempts to retrieve the result from the cache.
# If no match is found in the cache, then the query is run and
# the results are saved in the cache
get_cache = (collection, query, options) -> get_cache = (collection, query, options) ->
# Convert the query to a string to use as a key in the cache # Convert the query to a string to use as a key in the cache
query_string = JSON.stringify query query_string = JSON.stringify query
Expand All @@ -116,6 +121,7 @@ get_cache = (collection, query, options) ->
# Return the results # Return the results
models models


# This method get the unsorted results
get_models = (collection, query) -> get_models = (collection, query) ->


# Iterate through the query keys to check for any of the compound methods # Iterate through the query keys to check for any of the compound methods
Expand Down Expand Up @@ -177,6 +183,7 @@ page_models = (models, options) ->


sliced_models sliced_models


# If used on the server, then Backbone and Underscore are loaded as modules
unless typeof require is 'undefined' unless typeof require is 'undefined'
_ ?= require 'underscore' _ ?= require 'underscore'
Backbone ?= require 'backbone' Backbone ?= require 'backbone'
Expand All @@ -202,5 +209,6 @@ Backbone.QueryCollection = Backbone.Collection.extend
# Defined as a separate method to make it easy to bind to collection's change/add/remove events # Defined as a separate method to make it easy to bind to collection's change/add/remove events
reset_query_cache: -> @_query_cache = {} reset_query_cache: -> @_query_cache = {}


# On the server the new Query Collection is added to exports
unless typeof exports is "undefined" unless typeof exports is "undefined"
exports.QueryCollection = Backbone.QueryCollection exports.QueryCollection = Backbone.QueryCollection

0 comments on commit a255079

Please sign in to comment.