jeudi 24 août 2017

ember-cli-mirage skips the use of a serializer

i have a question about ember-cli-mirage.
i'm doing the frontend work in an application and setting up mirage to mock the backend while it's being made. my problem is the following:
i have two models (as an Ember and a Mirage model too) category and subcategory. category hasMany() subcategories while subcategories belongsTo() a category. now, the backend expects a GET request for subcategories to /api/v2/categories/:slug/subcategories so i made a serializer for category to modify the default Mirage behaviour from GET /api/v2/subcategories/:id. this is what i did last time i faced a similar API and it worked then.

here is the file:
mirage/serializers/category.js

import { JSONAPISerializer } from 'ember-cli-mirage';

export default JSONAPISerializer.extend({
  links(category){
    return{
      'subcategories': {
        related: `/api/v2/categories/${category.slug}/subcategories`
      }
    };
  }
});

but when running it, it still tries to fetch it from /subcategories/:id and gives the following error:

Mirage: Your Ember app tried to GET '/api/v2/subcategories/1',
         but there was no route defined to handle this request.
         Define a route that matches this path in your
         mirage/config.js file. Did you forget to add your namespace?

i only use subcategories in the category template:

category.hbs


  


and here is mirage/config.js

this.get('/categories');

this.get('/categories/:slug', function(schema, request) {
    let slug = request.params.slug;
    return findBySlug(schema.categories, slug, 'category');
  });

  this.get('/categories/:slug/subcategories', function(schema, request) {
    let slug = request.params.slug;
    let category = findBySlug(schema.categories, slug, 'category');
    return category.subcategories;
  });

//this.get('/subcategories/:id');

of course, if i enable the last line i don't get the error and get the data but not from where i want it.

any ideas or help would be very much appreciated!




Aucun commentaire:

Enregistrer un commentaire