我正在尝试使用Ember.computed.filter基于文本输入的输入来过滤ArrayController的内容.
App.IndexController = Ember.ArrayController.extend({
filteredPeople: Ember.computed.filter('model', function(person){
var searchFilter = this.get('searchFilter');
var regex = new RegExp(searchFilter, 'i');
return person.firstName.match(regex);
}),
// this is bound to the value of a text input helper
// I'm just pre-filling the value to show that the filtering does work
searchFilter: 'kris'
});
Run Code Online (Sandbox Code Playgroud)
这很好用,并按预期过滤.但是,我希望它随时更新值的更新searchFilter(即任何时候有人输入文本输入).现在的问题是,只有在对模型本身进行更改或控制器首次加载时才会更新.
无论如何都会在Ember.computed.filter更新时触发searchFilter更新?我正在使用,Ember.computed.filter因为它看起来比普通的计算属性更有效,我相信每次都会重新创建整个数组(如果我错了,请告诉我).另外,Ember.compute.filter简单易用,非常干净!
我用上面的设置创建了一个JSBin.当用户键入文本框时,我想以某种方式触发Ember.computed.filter更新自身.
感谢您的时间和任何帮助.
小智 9
也许太容易了,但是Ember.computed.filter使用.property()支持依赖键,所以这样可以正常工作:
App.IndexController = Ember.ArrayController.extend({
filteredPeople: Ember.computed.filter('model', function(person){
var searchFilter = this.get('searchFilter');
var regex = new RegExp(searchFilter, 'i');
return person.firstName.match(regex);
}).property('model','searchFilter'),
searchFilter: 'kris' //this is bound to the value of a text input helper
});
Run Code Online (Sandbox Code Playgroud)
jsbin:http://emberjs.jsbin.com/tefes/1/edit