Code Monkey home page Code Monkey logo

Comments (9)

samselikoff avatar samselikoff commented on May 16, 2024

defaultHandler being what gets called when there's no second param passed to this[verb]?

In your second case defaultHandler would then just return db.contacts, so you could write

this.get('/posts', function(db, request, defaultHandler) {
  var payload = db.contacts;
  payload.meta = { page: 1, timestamp: (new Date()).toString() };
  return payload;
});

(btw it's db instead of store now)

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 16, 2024

just to clarify, because the default handler changes based on the second param to this[verb] (undefined, array, string etc.)

from ember-cli-mirage.

cibernox avatar cibernox commented on May 16, 2024

Yes, with defaultHandler being the default handler of the route if no second argument is passed (although a this.get('/posts', 'article', function(db, request, handler) being handler the customized handler given the 2nd argument can also be useful)

In my second example it's not that simple as db.contacts, because the default handler takes care of coalesced requests like /posts?ids=1,3,5, while db.contacts returns all.

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 16, 2024

still confused...

because the default handler takes care of coalesced requests like /posts?ids=1,3,5, while db.contacts returns all.

bey default handler here are you referring to the first one that you wrote within your own mirage/config.js file?

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 16, 2024

might be easy if you just gist me the entire /mirage/config.js file, I haven't had a chance to use mirage with an app with lots of filtering/sorting so I'm not familiar with some of these pain points

from ember-cli-mirage.

cibernox avatar cibernox commented on May 16, 2024

I have a few routes that follow the same patterns, so I created a function that generates functions.

import config from '../config/environment';
var defaultPageSize = config.APP.defaultPageSize;

function servePaginatedAndCoalesced(type, { filterField }) {
  let normalizedType = type.underscore();
  let normalizedCollectionType = type.pluralize().underscore();

  return function(store, request) {
    let results = store.findAll(normalizedType);
    let ids = request.queryParams.ids;
    if (ids) {
      return { [normalizedCollectionType]: results.filter(e => ids.indexOf(''+e.id) > -1) };
    } else {
      let total   = results.length;
      let page    = 1;
      let filter  = request.queryParams.filter;
      if (filter && filter.length > 1) {
        filter  = filter.toLowerCase();
        results = results.filter(s => s[filterField].toLowerCase().indexOf(filter) > -1);
      }
      let payload = {};
      let pagedResults = results.slice(0, defaultPageSize);
      payload.meta = { total, page };
      payload[normalizedCollectionType] = pagedResults;
      return payload;
    }
  };
}

  this.get('/schools', servePaginatedAndCoalesced('school', { filterField: 'name' }));
  this.get('/teachers', servePaginatedAndCoalesced('teacher', { filterField: 'surname' }));
  this.get('/staff_members', servePaginatedAndCoalesced('staff_member', { filterField: 'surname' }));
  this.get('/students', servePaginatedAndCoalesced('student', { filterField: 'surname' }));
  // And some more

The real backend will a full featured elastic search filtering, but in mirage I am just creating some endpoints that:

  1. When requested without queryParams, return the first 10 elements in the collection and a meta on the root.
  2. When requested with ids=1,3,5, returns only the elements with those IDs (for enable coalescing)
  3. When receives a ?filter=abc qp and that filter string is long enough (>1 chars) I filter the results by some field.

The reason why I wanted to receive the defaultHandler is to avoid to implement the point 2 myself.

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 16, 2024

So I think one solution to this would be to define routes that differentiate on query params, for example:

this.get('/schools?ids=:ids', function(store, request) {
  let ids = request.queryParmas.ids;
  // query logic
});
this.get('/schools');  // default

Others have requested this too, e.g. to split up query logic across routes. Would this suit your purposes here? Or are you requesting a generic handler build-in to Mirage that understands ?ids=1,3,5?

from ember-cli-mirage.

cibernox avatar cibernox commented on May 16, 2024

That is good enough for most use cases 👍

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 16, 2024

Filing this under the "Calling super / expose default route handler" note in the roadmap

from ember-cli-mirage.

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.