Search on all app models


#1

Hi everybody,

I’m new to ember and I’m developing an app that has multiple models in different routes. I would like to implement a search feature that would be able to find the search term on all of my models. Do you think this can be done and if yes please describe a work flow.

Thank you, XY


#2

I know you can do this in many ways. I have not done this. But I use:

to search. I like this module. Hope someone can describe a workflow.


#3

@broerse thank you for your response!

I already use this addon and it is perfect.

queryResults: computedFilterByQuery('results', 'name', 'typedText')

The results param is a static array at the moment, if only i could find a way to populate results param with all app models.


#4

You could just peekAll for the different record types you care to search. If you want to dig around private apis Ember store also has a record array manager that you may be able to hack into.


#5

That is what i’m trying to implement but i’m failing. The queryResults are populated inside a component so i’m trying to figure out how to peekAll/fetchAll inside that component and feed it to the computedFilterByQuery.

Any ideas?


#6

One way you could do it that may not be very performant is add arrays for each of the record arrays you want

const { Component, computed, inject } = Ember;
const { union } = computed;
const { service } = inject;

function modelPeek(modelType){
  return computed(function(){ this.get('store').peekAll(modelType); });
}

MyComponent = Ember.Component.extend(
  store: service(),

  models1: modelPeek('model1'),
  models2: modelPeek('model2'),

  allModels: union('models1', 'models2'),
)

#7

Thank you for your reply.

What i ended up doing is populated my model in route with an RSVP

 model: function() {
     return Ember.RSVP.hashSettled({
         model-one: store.findAll('model-one'),
         model-two: store.findAll('model-two'),
     });
 }

and then injected it to the component

{{search model=model}}

finally utilized it like so

 queryResults: computedFilterByQuery('model', 'fieldtosearch', 'typedText')
                     .readOnly(),

Now my major issue is that model-one and model-two have a completely different "model schema" so i have to find a way when the RSVP gets “Settled” to process them and return to the model property a “normalized schema”.

Any ideas?


#8

RSVP.all( [model one records, model two records]).then(function(results){ do whatever normalization you want })


#9

Yes! you are right i’m there but i can’t quite figure it out yet.

using this

model: function() {
    var store = this.store;
    return Ember.RSVP.hashSettled({
        model-one: store.findAll('model-one'), model-two: store.findAll('model-two')
    }).then(function(results){
        console.log(results);
        for(var type in results){
            console.log(results[type].value.content);
        }
        return results;
    }, function(reason){
        console.log(reason);
    });

i get objects like the following

{
  menu-one: Object
     state: "fulfilled"
     value: {
        content: ...
        isLoaded: true
        isUpdating: false
        manager: ...
        store: ...
        type: ...
        ....
     }
}

i can’t find a way to access the raw data from the above object, in order to get a particular field and populate a new object and then return it to the model property.