davidgtonge / backbone_query Goto Github PK
View Code? Open in Web Editor NEWA lightweight query api for Backbone Collections
License: MIT License
A lightweight query api for Backbone Collections
License: MIT License
There is a typo in the docs.
MyCollection.query({ likes: {$between:[5,15} });
It should be
MyCollection.query({ likes: {$between:[5,15]} });
instead.
I admit to not being very familiar with requirejs but I'm running into some issues trying to load up backbone_query. I'm using backbone boilerplate as a base and I'm unable to get Backbone.QueryCollection to be defined. Is there any pointers?
if multiple tests are to be performed on the same key, then the process will fail
The following example from .readme will not return any results. Are there any plans to update this library so it is compatible with latest versions of backbone???
Thanks,
Justin
MyCollection.query({
$or:[
{title:"News"},
{title:"About"}
]
});
// Returns all models with the title "News" or "About"
``
Maybe I am missing something obvious but I am trying to store the actual query part in a db as a saved search and then use it to filter the collection.
Following your example:
MyCollection.query(
{tags: { $any: ["coffeescript", "backbone", "mvc"]}},
{sortBy: "likes", order: "desc", limit:10, page:2, cache:true}
);
If I take the main part out:
{tags: { $any: ["coffeescript", "backbone", "mvc"]}},
{sortBy: "likes", order: "desc", limit:10, page:2, cache:true}
That isn't valid json. Do you have any better ideas on how to store this in a db to be used later?
I was trying to store something like the following and merge the two parts together but isn't very clean and never could get it to work well:
{"query": [{"$and": {"tags": {"$all": ["!1"]},"status": "waiting"}}],"sorting": [{"sortBy": "last_reply_at", "order": "desc"}]}
Here is an actual query that works directly just not parsing via parseJSON
{$and: {tags: { $all: ["!1"] } }}, {sortBy: "last_reply_at", order: "desc", status:"waiting"}
First off, this project is awesome.
I have data that looks like this:
courses = [
{
title: "Biology",
assignments: [
{
done: false,
due: "2012-03-05T05:00:00.000Z",
},
{
done: false,
due: "2012-03-05T05:00:00.000Z",
},
{
done: true,
due: "2012-03-06T05:00:00.000Z",
}
]
},
{
title: "Math",
assignments: [
{
done: true,
due: "2012-03-05T05:00:00.000Z",
},
{
done: false,
due: "2012-03-03T05:00:00.000Z",
},
{
done: true,
due: "2012-03-06T05:00:00.000Z",
}
]
}
So basically a collection of courses which have a list of assignments.
I'm using backbone-relational to model this; courses is a QueryCollection, the individual course model is a RelationalModel, and the individual assignment model is a RelationalModel as well.
The query I want to do is to find all of the assignments (in all courses) that are due within a specific range and are not done.
From the examples (and being somewhat familiar with Mongo -- it's what I use on the server) I think that would look like this:
courses.query({
assignments: {
$elemMatch: {
"done": true,
"date": "2012-03-05T05:00:00.000Z"
}
}
});
(I'm just matching one specific date, not a range, for simplicity purposes).
The problem is that the above query does not return anything. It seems as if it's not applying the $elemMatch criteria to the 'assignments' field of each course.
Any idea why that might be? Feel free to call me names if I'm doing something incredibly dumb here -- my brain is a bit fried from staring at my computer all day ;)
Thanks!
It would be nice to have an out-of-the-box benchmark to run using node benchmarks.js
using Benchmark.js.
I'm using this library on a AngularJS project and angularjs is known of firing the same cycles over and over again, just one page load, a $watcher is called 90 times!
so I'm interested to I can rest assured that this won't ever be a bottleneck.
Basic logic is not functioning in your API, see these tests: http://jsfiddle.net/RZJcJ/
Col = new Backbone.QueryCollection [
{diff:1, equ:'ok', same: 'ok'},
{diff:2, equ:'ok', same: 'ok'}
]
# Should return all items, but returns nothing
Col.query
$and:
equ: 'ok'
$or:
same: 'ok'
# Should return nothing, but all items are returned:
Col.query
equ: 'bogus' # This item is clearly unavailable
$or:
same: 'ok'
Just a note for others, so that they don't waste valuable time on debugging this:
Backbone-query does not work out-of-the box in conjunction with the RequireJS optimizer,
define ['underscore', 'backbone'], (_, Backbone) ->
Backbone
(to export the module variable).(function(){
with `define(['underscore', 'backbone'], function(_, Backbone) {}).call(this);
with });
(or, if an export is desired, return Backbone;});
. Don't worry about the changed context, Backbone-query doesn't use var root = this;
or something to export variables.If you want to create a module which is compatible with node and AMD loaders, have a look at these examples for Backbone and underscore: https://gist.github.com/2517531 :
(function(root, define) { define('underscore', function(require, exports, module) {
...
})}).call(this, this, typeof define === 'function' && define.amd ? define : function(id, factory) {
if (typeof exports !== 'undefined') {
// CommonJS has require and exports, use them and execute
// the factory function immediately. Provide a wrapper
// for require to deal with jQuery.
factory(function(id) {
// jQuery most likely cannot be loaded
// in a CommonJS environment, unless the developer
// also uses a browser shim like jsdom. Allow
// for that possibility, but do not blow
// up if it does not work. Use of a
// try/catch has precedent in Node modules
// for this kind of situation.
try {
return require(id);
} catch (e) {
// Do not bother returning a value, just absorb
// the error, the caller will receive undefined
// for the value.
}
}, exports);
} else {
// Plain browser. Grab the global.
this._ = {};
}
});
Given a collection consisting of models like
{
"title": "post title",
"content": "post content",
"id": "1",
"comments": [
{
"text": "comment 1 content"
},
{
"text": "comment 2 content"
}
]
}
I'd like to be able to query against the text inside the nested array, in addition to the text in the toplevel attributes. Currently I'm doing this server-side using a mongo query against the nested property like:
find(
"$or"=>[
{:title => /#{query}/i},
{:content => /#{query}/i},
{"comments.text" => /#{query}/i}
]
)
Is it possible or planned to approximate this behavior with backbone_query?
I am building my querys dynamically. Therefore It would be great if backbone_query had the ability to check two or more times on the same argument. Meaning that these are the same:
MyCollection.query({ likes: {$gte:5}, likes: {$lte:15} });
MyCollection.query({ likes: {$between:[5,15} });
I can´t get it to work. This does not work either:
MyCollection.query({
$and:{
likes: {$gte: 5 },
likes: {$lte: 15 }
}});
I have been using nested models quite a lot (using backbone-relational) to replicate the server side models in Backbone. This seems to be a really interesting project.
It would be even awesome if there would be support for nested models. Atm, it seems like i can perform queries only on categories(of Models in the collection), but what if my models had other nested models (
Lets say Comment Model -> belongs to -> Blog Model -> belongs to -> User Model.
Given the Comment Collection, I'd like query and list all the comments that belongs to a User.
Any plans for such support ?
Please add support for bower.
Hey,
I'm trying to create a dynamic query, but it just doesn't seem to want to work, even though the query I'm generating works when I put it in statically. No errors are being thrown... Everything works except the dynamic query.
query = {};
for(i in selected) {
if(selected.hasOwnProperty(i)) {
var terms;
if(selected[i].indexOf(',') >= 0) {
terms = {$all: selected[i].split(',')};
} else {
terms = selected[i];
}
}
query[i] = terms;
}
result = this.collection.query(query);
console.log(result);
the line on whereBy
:
return new this.constructor(this.query(params, options));
is adding to the model _events.all
, many callbacks, and the array grows until the app start to feel laggy or unresponsive. I'm not sure what would the fix be. I'm using {cache: true}
, but that doesn't seem to make a difference.
Here is the reproducible test case
Toggle the items, and press the button. Each time the button is pressed, a whereBy query is performed, and another item is appended to each model _events array. So, right now, I'm having to use underscore functions instead of chaining using whereBy
Very similar to Query Engine perhaps we should team up :) What are your thoughts?
Here is a comparison that I've done up:
author:ben priority:important
)A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.