74390f2d24
This does several things to make improving the search experience easier moving forwards: 1. Separate searching off from filtering. 'Searching' can be thought of as specifically 'text searching' whilst filtering is more of a boolean/flag search. 2. Decouple the actual searching functionality to almost pure, isolated / unit testable units and unit test. (I still import embers get which, once I upgrade to 3.5, I shouldn't need) 3. Searching rules are now configurable from the outside, i.e. not wrapped in Controllers or Components. 4. General searching itself now can use an asynchronous approach based on events. This prepares for future possibilities of handing off the searching to a web worker or elsewhere, which should aid in large scale searching and prepares the way for other searching methods. 5. Adds the possibility of have multiple searches in one template/route/page. Additionally, this adds a WithSearching mixin to aid linking the searching to ember in an ember-like way in a single place. Plus a WithListeners mixin to aid with cleaning up of event listeners on Controller/Component destruction. Post-initial work I slightly changed the API of create listeners: Returning the handler from a `remover` means you can re-add it again if you want to, this avoids having to save a reference to the handler elsewhere to do the same. The `remove` method itself now returns an array of handlers, again you might want to use these again or something, and its also more useful then just returning an empty array. The more I look at this the more I doubt that you'll ever use `remove` to remove individual handlers, you may aswell just use the `remover` returned from add. I've added some comments to reflect this, but they'll likely be removed once I'm absolutely sure of this. I also added some comments for WithSearching to explain possible further work re: moving `searchParams` so it can be `hung` off the controller object
33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
import Mixin from '@ember/object/mixin';
|
|
import { inject as service } from '@ember/service';
|
|
import { get, set } from '@ember/object';
|
|
import WithListeners from 'consul-ui/mixins/with-listeners';
|
|
/**
|
|
* WithSearching mostly depends on a `searchParams` object which must be set
|
|
* inside the `init` function. The naming and usage of this is modelled on
|
|
* `queryParams` but in contrast cannot _yet_ be 'hung' of the Controller
|
|
* object, it MUST be set in the `init` method.
|
|
* Reasons: As well as producing a eslint error, it can also be 'shared' amongst
|
|
* child Classes of the component. It is not clear _yet_ whether mixing this in
|
|
* avoids this and is something to be looked at in future to slightly improve DX
|
|
* Please also see:
|
|
* https://emberjs.com/api/ember/2.12/classes/Ember.Object/properties?anchor=mergedProperties
|
|
*
|
|
*/
|
|
export default Mixin.create(WithListeners, {
|
|
builder: service('search'),
|
|
init: function() {
|
|
this._super(...arguments);
|
|
const params = this.searchParams || {};
|
|
this.searchables = {};
|
|
Object.keys(params).forEach(type => {
|
|
const key = params[type];
|
|
this.searchables[type] = get(this, 'builder').searchable(type);
|
|
this.listen(this.searchables[type], 'change', e => {
|
|
const value = e.target.value;
|
|
set(this, key, value === '' ? null : value);
|
|
});
|
|
});
|
|
},
|
|
});
|