-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More array functions for models / collections #51
Comments
Yes – we actually used to do this but what makes it hard is that those methods don't necessarily return Collections (because you can return anything from them). A Collection needs a It was also causing issues because folks thought collections were real JS arrays and they were trying to use Lodash methods on them and they were breaking in unexpected ways. I'm not sure what I would do today + whether there's now a way to truly actually subclass arrays in JS. Today the escape hatch is to do |
thats what I currently use at the moment
and using alternative function names? |
Yeah we could I'm just hesitant because it feels like extra APIs to support, it's something new to learn, and folks could still get confused and try to use libs like lodash on them. Could you share with me your use case (the last time you used one of those methods)? |
I did a prototype for an App-Store, just to refresh some Angular basics. If I had such array function it would be a little easier to achieve. Or I totally missed something about calculated fields in the docs 😁 |
I would use the afterCreate factory hook along with a trait for this: import { Server, Model, Factory, trait } from 'miragejs';
new Server({
models: {
app: Model.extend({
ratings: hasMany()
}),
rating: Model
},
factories: {
app: {
withRatings: trait({
afterCreate(app, server) {
app.update({
ratings: server.createList('rating', 3, { app })
})
}
})
}
},
seeds(server) {
server.createList('app', 4, 'withRatings') // boom, 4 apps with ratings
}
}) Of course that |
I know, but the problem wasn't to create the related ratings but to add a calculated field in each app for the total rating e.g.
those array functions would make it more easy to iterate over the ratings of an app to calculate such a field |
Ahh I see. Yes, unfortunately we don't have good support for this right now, it's definitely a gap in Mirage that will be closed (sooner rather than later because people keep running into it). Does it have to be on the model or just the response? If response, I would use the Serializer layer to add computed/derived fields. Most likely in the |
Related #45 |
The collection already has
filter
,slice
andsort
.But it would be nice to also have
map
,forEach
(could also beeach
) andreduce
.The text was updated successfully, but these errors were encountered: