Comments (9)
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.
just to clarify, because the default handler changes based on the second param to this[verb] (undefined, array, string etc.)
from ember-cli-mirage.
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.
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.
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.
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:
- When requested without queryParams, return the first 10 elements in the collection and a
meta
on the root. - When requested with
ids=1,3,5
, returns only the elements with those IDs (for enable coalescing) - 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.
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.
That is good enough for most use cases 👍
from ember-cli-mirage.
Filing this under the "Calling super / expose default route handler" note in the roadmap
from ember-cli-mirage.
Related Issues (20)
- Remove the testConfig function HOT 5
- Consider making MirageJS a peer dependency HOT 12
- Mirage not intercepting requests HOT 1
- discoverEmberDataModels() triggers `ember-data:deprecate-early-static` deprecation HOT 4
- What this addon does now that MirageJS has been extracted HOT 4
- v3 upgrade guide? HOT 1
- Timing not working in miragejs server instance HOT 1
- breaks when using ember-data 4.8.0 HOT 15
- Extending a model breaks one-to-many relationship
- Mirage cannot preload assets HOT 2
- New Release? HOT 14
- discoverEmberDataModels() fails if any pre Octane style Ember model has a property named `errors` HOT 1
- Tests only? HOT 9
- Is it possible to have a custom folder for fixtures in mirage? HOT 2
- Accessing Models using `require()` will no longer work on ember-data v5 HOT 1
- `discoverEmberDataModels` crashes if there is a non ember-data object in the app/models folder HOT 6
- mirage enabled even when using `--proxy` flag HOT 5
- Polymorphics - missing the buid method HOT 2
- Memory Leak codemod is suggesting some changes in mirage files
- afterCreate no longer has mirage models HOT 1
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 ember-cli-mirage.