Code Monkey home page Code Monkey logo

Comments (8)

samselikoff avatar samselikoff commented on July 22, 2024 1

Related #45

from discuss.

samselikoff avatar samselikoff commented on July 22, 2024

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.

NonPolynomial avatar NonPolynomial commented on July 22, 2024

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.

samselikoff avatar samselikoff commented on July 22, 2024

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.

NonPolynomial avatar NonPolynomial commented on July 22, 2024

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.

samselikoff avatar samselikoff commented on July 22, 2024

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.

NonPolynomial avatar NonPolynomial commented on July 22, 2024

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.

samselikoff avatar samselikoff commented on July 22, 2024

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)

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.