此代码用于搜索hasMany儿童的工作就像一个魅力。但我希望在当前模型中搜索(例如按商店名称过滤:&#39; storeOne&#39;),这是因为我想在当前模型中搜索,而不是查询到this.store而不是查询到api ... < / p>
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);
});
});
更新:
修复我的问题,过滤当前的模型项而不请求新数据 来自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')
答案 0 :(得分:1)
如果我理解你的问题,你不想要求后端检索模型的孩子吗?
如果正确,以下内容应该适合您。它是从Ember doc中提取的。
使用store.peekRecord()按类型和ID检索记录,而不发出网络请求。仅当记录已存在于商店中时,才会返回记录。