GithubHelp home page GithubHelp logo

Comments (4)

prust avatar prust commented on July 22, 2024

@paglias,

The docs state:

Note that get will only ever return a single model, so the values of these indexes should be unique across the models in the collection

I'm assuming that you don't want to change that (correct me if I'm wrong), but that you want to pass in an array of IDs as your query and get multiple models back, like this:

var models = coll.find([1, 5, 7]);
// would return 3 models

I'm not a fan of the name find() since this is already a method in Underscore/Backbone and it returns a single model.

Personally I would prefer to take the concept a bit further, using underscore/backbone's where() API, but having a special exception if it is passed an array. Instead of looking for that same array on the models it is searching, it would match any model that has any of the values in the array. For example:

var models = coll.where({id: [1, 5, 7]});
// would return 3 models

But this is opinionated and not 100% obvious and it makes it impossible to use where() to search for an array reference (though over the course of years of backbone programming I've never needed this). Implementing this kind of smart query engine that uses indexes wouldn't be hard, but seems a bit beyond the scope of ampersand-collection... it's something that could easily be implemented on top of ampersand-collection using the existing .get() and .indexes APIs.

from ampersand-collection.

paglias avatar paglias commented on July 22, 2024

@prust so I think I missed the part where it says that index should be considered unique :)

My idea was to implement the equivalent of Backbone's where but using indexes for fast access.

Indexes would be moved from single references to an array of references with .get() returning the first one only.

As for a standard where, personally I'm using collection.filter to achieve the same result but I think it would be a valuable addition too.

Lastly, I don't have any preference for the name, I said find() because I noticed it's used sometimes to distinguish from returing a single item (get) or multiple (find)

from ampersand-collection.

prust avatar prust commented on July 22, 2024

@paglias:

Indexes would be moved from single references to an array of references

Ah, I see. That would make indexes more valuable and would need to be an internal change to ampersand-collection. Perhaps that should be a separate (and more important) issue, with the .find()/.where() method being a secondary issue that could be implemented separately as a mixin...

from ampersand-collection.

pgilad avatar pgilad commented on July 22, 2024

Use collection.filter(model => _.includes([1, 2, 3], model.id);

from ampersand-collection.

Related Issues (20)

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.