2015-12-22 96 views
0

此代碼用於搜索hasMany兒童的工作就像一個魅力。但我想在目前的模式(例如,通過店名過濾:「storeOne」)進行搜索,這是因爲原因,我想在目前的模式進行搜索,沒有查詢到this.store並沒有查詢到API ...Ember在當前模型中搜索

 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?

UPDATE:

  • 解決我的問題,從this.store或服務器API,而不要求過濾電流模型項目的新數據 。

    filtered: Ember.computed.filter('[email protected]', 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('[email protected]', 'searchString') 
    

回答

1

如果我明白你的問題,你不想讓到後端檢索模型的孩子的要求?

如果是正確的,以下內容應該適合您。它是從Ember文檔中提取的。

使用store.peekRecord()按類型和ID檢索記錄,而不發出網絡請求。這隻會在記錄已經存在時纔會返回記錄。

+0

對不起,但你想念..所有的孩子在商店。我想過濾器模型的孩子,而不是從商店,API ... – iTux