Comments (8)
Related #45
from discuss.
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 modelName
property on it so it can be properly serialized.
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 collection.models
to access the underlying JavaScript Array of models and go to town on it.
from discuss.
Today the escape hatch is to do collection.models to access the underlying JavaScript Array of models and go to town on it.
thats what I currently use at the moment
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.
and using alternative function names?
e.g. forEach
-> each
from discuss.
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. .models
or toArray()
feels like a decent middleground.
Could you share with me your use case (the last time you used one of those methods)?
from discuss.
I did a prototype for an App-Store, just to refresh some Angular basics.
So i had the models App
and Rating
and the relation App --|1:n|-> Rating
I read in the documentation that association
just works with belongTo
but not with hasMany
and I wanted to create random ratings for my apps when I call server.createList('app', 4)
and to add a calculated field of the total rating, that the "frontend" doesn't have to calculate it itself.
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 😁
from discuss.
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 afterCreate
hook can do anything, make a random number of ratings, use other traits on the rating
factory and so on.
from discuss.
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.
App
- ratings:
- rating: 5 stars
- rating: 4 stars
- rating 3 stars
- totalRating: 4 stars
those array functions would make it more easy to iterate over the ratings of an app to calculate such a field
from discuss.
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 serialize()
hook https://miragejs.com/api/classes/serializer/#serialize
from discuss.
Related Issues (20)
- Support multiple server instance. HOT 5
- Add Middleware support to Mirage HOT 1
- Be able to mock websocket calls HOT 2
- Custom exception post-processing HOT 2
- Question: Expose mocked API as standalone on a specific port? HOT 3
- Tracking issue for routing DSL / passthrough / namespace improvements
- nativescript-vue with miragejs HOT 1
- Model hooks / default attributes HOT 5
- hasMany to hasMany (manyToMany) fixture associations not created HOT 1
- Passthrough external requests with wildcard HOT 4
- cannot use trait with afterCreate HOT 8
- routes for related resources HOT 2
- [Suggestion] Automatically parse request body based on content type HOT 2
- Feature Request: Generate API docs from test runs HOT 3
- How to only intercept request that defined in routes HOT 2
- server.createList() and server.create() creates duplicate values with dynamic factory definition
- Is it possible to use a combination of mock API's and actual API calls ? HOT 1
- Using a nested primary id for update and deletes
- Adding context object to handler to mock auth
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 discuss.