如何在Emberjs中仅返回过滤后的模型?

时间:2013-09-24 19:19:01

标签: javascript ember.js

我有一个Ember.Select,它根据某些角色过滤模型。此时,显示应用程序启动模型的完整内容的时间,但我想仅在应用过滤器时显示模型。我对如何做到这一点毫无头绪。

这是问题中的控制器,

App.TwodController = Ember.ArrayController.extend({
    //filteredContent : null,
    sortProperties: ['firstname'],
    sortAscending: true,
    selectedExperience : null,
    experience : [{
        exp : "1"
    }, {
        exp : "2"
    }, {
        exp : "3"
    }, {
        exp : "4"
    }, {
        exp : "5"
    }],
    selectedDesignation : null,
    filterDesignation : function() {
        var designation = this.get('selectedDesignation.designation');
        var filtered = this.get('content').filterProperty('designation', designation);
        this.set("filteredContent", filtered);
    }.observes('selectedDesignation'),
    designations : [{
        designation : "Design",
        id : 1
    }, {
        designation : "Writer",
        id : 2
    }, {
        designation : "Script",
        id : 3
    }, {
        designation : "Storyboard",
        id : 4
    }, {
        designation : "Workbook",
        id : 5
    }],
    actions : {
        filterExperience : function() {
            var experience = this.get('selectedExperience.exp');
            var filtered = this.get('content').filterProperty('experience', experience);
            this.set("filteredContent", filtered);
        },
        refresh : function() {
            var refresh = this.get('content');
            this.set("filteredContent", refresh);
        }
    },
    filteredContent : function() {
        var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');

        return this.get('model').filter(function(item) {
            var fullname = item.firstname + item.lastname;
            return regex.test(fullname);
        });
    }.property('searchText', 'model')

});

此外,我遇到的另一个问题是我无法按升序排序。我需要在代码中进行哪些更改才能获得所需的结果?

如果有人感兴趣的话,这是完整的JSBin

1 个答案:

答案 0 :(得分:1)

App.TwodController包含:

init: function() {
  this.set('filteredContent', []);
},

filterDesignation根据the Ember.SortableMixin documentationthis.get('content')更改为this.get('arrangedContent')

filterDesignation : function() {
    var designation = this.get('selectedDesignation.designation');
    var filtered = this.get('arrangedContent').filterProperty('designation', designation);
    this.set("filteredContent", filtered);
}.observes('selectedDesignation'),