0

This code for search in hasMany childrens work like a charm. But i want search in current model (e.g. filtered by store name: 'storeOne'), that is reason because i want search in current model, not query to this.store and not query to api...

        var _self = this;
        this.store.findAll('store').then(function(stores){

            // search
            var promises = stores.map(function(store){

                return Ember.RSVP.hash({
                    store: store,
                    customers: store.get('customers').then(function(customers){
                        return customers.filter(function(customer){
                            var regExp = new RegExp(search, 'i');

                            var retVal = false;

                            if (customer.get('name') !== undefined ) retVal = retVal || customer.get('name').match(regExp);
                            if (customer.get('surname') !== undefined ) retVal = retVal || customer.get('surname').match(regExp);
                            if (customer.get('age') !== undefined ) retVal = retVal || customer.get('age').match(regExp);

                            return retVal;
                        });
                    })
                });

            });

            Ember.RSVP.all(promises).then(function(filteredData){
                _self.set('content', filteredData);
            });
        });
  • Question: How can i filter by search customers in current model without use findAll or query to API ?

UPDATE:

  • Fix my question, filter current model items without request new data from this.store or server api.

    filtered: Ember.computed.filter('model.@each.store', function(store){    var search = this.get('searchString');
    
    if (search !== undefined) {
    
        guild.get('customers').then(function(customers) {
    
            var regExp = new RegExp(search, 'i');
    
            customers.map(function(customer){
    
                var retVal = false;
    
                var name = customer.get('name');
                var surname = customer.get('surname');
                var age = customer.get('age');
    
    
                if (name !== undefined ) {
                    retVal = retVal || name.match(regExp) !== null;
                }
                if (nick !== undefined ) {
                    retVal = retVal || surname.match(regExp) !== null;
                }
                if (age !== undefined ) {
                    retVal = retVal || age.match(regExp) !== null;
                }
    
                customer.set('show', retVal);
            });
    
        });
    } else {
        guild.get('customers').then(function(customers) {
            customers.map(function(customer){
                customer.set('show', true);
            });
        });
    }
    
    return store; }).property('model.@each.store', 'searchString')
    
iTux
  • 1,946
  • 1
  • 16
  • 20

1 Answers1

1

If i understand your question you don't want to make a request to backend to retrieve child of a model?

If it's right the following should be ok for you. it's extract from the Ember doc.

Use store.peekRecord() to retrieve a record by its type and ID, without making a network request. This will return the record only if it is already present in the store.

Scandinave
  • 1,388
  • 1
  • 17
  • 41
  • Sorry, but you miss.. All child in store. I want filter model child and not get in from store, api... – iTux Dec 23 '15 at 06:14