0

这个在 hasMany childs 中的搜索代码就像一个魅力。但我想在当前模型中搜索(例如按商店名称过滤:'storeOne'),这是因为我想在当前模型中搜索,而不是查询 this.store 而不是查询 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);
            });
        });
  • 问题:如何在不使用 findAll 或查询 API 的情况下按当前模型中的搜索客户进行过滤?

更新:

  • 解决我的问题,过滤当前模型项而不从 this.store 或服务器 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')
    
4

1 回答 1

1

如果我理解您的问题,您不想向后端请求检索模型的子节点吗?

如果它是正确的,那么以下对你来说应该没问题。它是从 Ember 文档中提取的。

使用 store.peekRecord() 按类型和 ID 检索记录,无需发出网络请求。只有当它已经存在于商店中时,这才会返回记录。

于 2015-12-22T21:28:51.700 回答