Comments (4)
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.
@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.
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.
Use collection.filter(model => _.includes([1, 2, 3], model.id);
from ampersand-collection.
Related Issues (20)
- Fix performance issues in collections and subcollections HOT 10
- deIndex is being called wrong HOT 8
- -nothing-to-see-here- HOT 1
- Tracking collection state. HOT 13
- .get(id) can't find object with id 0 HOT 2
- using ES5 `bind` for double argument comparator causes Phantom.js to fail HOT 2
- adding element with id:0 results in duplicates
- Upgrade from 1.4.2 to 1.4.3 causes jasmine tests to fail HOT 3
- Consistent sorting functions HOT 4
- Collection of Collections HOT 3
- index issue during collection.set
- Iterable via for...of HOT 1
- Document `parse` method HOT 1
- Collection does not sort itself when elements change HOT 4
- No good hook for when models are inserted
- No "update" event like in Backbone
- lodash helper do not support second parameter binding HOT 1
- Feature Proposal: "cancel" callback in fetch options? HOT 1
- When null is passed as value, it instantiates an empty model in the collection HOT 2
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 ampersand-collection.