Emberjs:如何一次过滤多个房产

KAL*_*LBB 8 javascript ember.js ember-data

下面我肯定会过滤一个属性,但是如何一次性过滤?也就是说,不向用户提供包含不同搜索选项的下拉列表 示例:我的搜索词可能是姓名,电子邮件或年龄.

var search = this.controllerFor('employees').search; //can be name, email or age

employees = this.get('currentModel').filterProperty('name', search);
Run Code Online (Sandbox Code Playgroud)

以上工作可以很好地更新主列表,但我只能一次过滤一个属性.

//Sample Model
App.Employee = DS.Model.extend({
    email: DS.attr('string'),
    name: DS.attr('string'),
    age: DS.attr('number'),
})
Run Code Online (Sandbox Code Playgroud)

一种想法是如果过滤器结果再次重新过滤,length = 0并且一些如何合并结果.但是,我对这个想法并不陌生,并且相信Ember可能会有更好的 - 更优雅的方式实现这一目标.

Mil*_*Joe 8

您可以使用该filter函数过滤模型中的多个属性,甚至可以使用控制器中的其他属性.例如:

想象一下这样的模型:

App.Person = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    fullName: function() {
        return '%@ %@'.fmt(
            this.get('firstName'),
            this.get('lastName')
        );
    }.property('firstName', 'lastName')
});
Run Code Online (Sandbox Code Playgroud)

要按多个属性进行过滤,假设您的控制器具有与此类似的搜索功能:

...
performSearch: function(searchTerm) {
    return this.get('content').filter(function(person) {
        return person.get('firstName').indexOf(searchTerm) !== -1 ||
               person.get('lastName').indexOf(searchTerm) !== -1;
    });
},
...
Run Code Online (Sandbox Code Playgroud)

这将遍历联系人列表content并应用一个或多个过滤器,仅返回与过滤器对应的模型对象.

小提琴:http://jsfiddle.net/schawaska/ABJN7/