Git Product home page Git Product logo

Comments (5)

davidgtonge avatar davidgtonge commented on May 11, 2024

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.

suvash avatar suvash commented on May 11, 2024

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.

suvash avatar suvash commented on May 11, 2024

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.

suvash avatar suvash commented on May 11, 2024

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.

davidgtonge avatar davidgtonge commented on May 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.