Comments (5)
It would be difficult to implement this in a generic way, however it could be achieved quite simply by using the $cb
operator and a custom function for the comment model. Here is an example in coffeescript:
Comment = Backbone.RelationalModel.extend
relations:[] #Standard Backbone Relational settings
getUserID: -> @get("post")?.get("user")?.id
Comments = Backbone.QueryCollection.extend
model:Comment
getByUser: (user_id) ->
@query
user:
$cb: -> @getUserID() is user_id
If you have any ideas on a neater way to implement this, please let me know.
from backbone_query.
Agree with the fact that it would be hard to implement it in a generic way. And this was the approach I was going for.
atm, cannot think of any other way to implement this other than lets say, perhaps adding a separate query method (or a standard argument passed to the query method) for nested objects that could create and use the getUserID behind the scenes. but then I'm just thinking right now.
Either ways, thanks for the help ! Very appreciated.
from backbone_query.
Once again, what would you recommend if I wanted to check for more conditions ?
In the above case, what if I wanted to list all the comments that belongs to more than one user. That was in fact, the first reason I got started with the library. lets say something like following in your example, but with the callback.
MyCollection.query({ title: {$in:["About", "Home", "Contact"] } });
// Returns all models which have a title attribute of either "About", "Home", or "Contact"`
from backbone_query.
ok, I figured out a solution. do let me know if you see anything funny in this code.
Comments = Backbone.QueryCollection.extend
model:Comment
getByUser: (user_id) ->
@query
user:
$cb: -> @getUserID() is user_id
getByUsers: (users_id) -> # users_id is an array of id of users
comments = new Comments
comments.add @getByUser(user_id) for user_id in users_id
comments
from backbone_query.
Hi, if I understand what you need to do, I'd do it simply as follows
Comments = Backbone.QueryCollection.extend
model:Comment
getByUser: (user_id) ->
@query
user:
$cb: -> @getUserID() is user_id
getByUsers: (user_ids) ->
@query
user:
$cb: -> @getUserID() in user_ids
But if you aren't checking for multiple conditions or using the paging / caching features of Backbone.Query, then you are probably better off just using a filter like this:
Comments = Backbone.QueryCollection.extend
model:Comment
getByUser: (user_id) ->
@filter (model) -> model.getUserID() is user_id
getByUsers: (user_ids) ->
@filter (model) -> model.getUserID() in user_ids
Note that both of these methods are using CoffeScripts helpful 'in' operator (http://coffeescript.org/#operators)
from backbone_query.
Related Issues (16)
- Storing the query part in a db HOT 2
- requirejs + backbone_query HOT 4
- two or more checks on same argument HOT 2
- Typo id docs HOT 1
- dynamic queries not working? HOT 1
- whereBy is making the _events array leak HOT 7
- Implement benchmark.js? HOT 1
- Add bower support HOT 1
- multiple queries against same key in array no longer working (backbone version ~1)
- Search on nested properties HOT 7
- $elemMatch doesn't work with backbone-relational? HOT 5
- Very similar to Query Engine HOT 10
- need to use arrays rather than objects for query object
- Making Backbone-query AMD-compatible
- Combined $and and $or are not working HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backbone_query.