我是 Ember 的新手,正在尝试为 EmberJS 中的项目数组编写一个简单的搜索/过滤器,但它似乎比看起来更难。我找到了一个类似的 question here ,但我似乎无法将搜索输入绑定(bind)到函数。我的示例是 EmberJS 网站上的默认 Todo 应用程序。
<script type="text/x-handlebars" data-template-name="todos">
{{input type="text" id="search-todo" placeholder="Search Todo List..." value=searchTerm}}
.....
</script>
在我的 App.TodosController
中有
searchResult: function(){
var searchTerm = this.get('searchTerm');
if(!searchTerm.trim()){return;}
var regExp = new RegExp(searchTerm);
return this.get('content').filter(function(item){
return regExp.test(item.get('title'));
});
}.property('searchTerm','@each.title')
但是当我输入时,没有任何反应。我尝试将 searchResult
添加到 Controller 的 actions
散列中,当我将 action= searchResult
添加到脚本标签,但我没有任何反应。基本上,我只想要一个简单的 filtering of the list like AngularJS ,我希望它不仅在 title
上,而且在所有内容上,我不需要它是一个单独的路由,如果它必须在一个单独的路由中,我仍然不不知道如何实现这一点。
http://jsbin.com/AViZATE/20/edit
感谢您的帮助。
最佳答案
我能够通过添加 observes
关键字来解决它。
这是解决方案的链接 http://jsbin.com/AViZATE/37
searchResult: function(){
var searchTerm = this.get('searchTerm');
var regExp = new RegExp(searchTerm,'i');
this.get('model').set('content',this.store.filter('todo',function(item){
return regExp.test(item.get('title'));
}));
}.observes('searchTerm')
在 Html 中我有
{{input type="text" id="search-todo" placeholder="Search Todo List..." value=searchTerm}}
关于javascript - EmberJS 过滤项目数组作为用户类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18812401/