[英]I want Ember.js' {{input value=“searchTerm”}} to set its value to ' ' when the URL changes
我會盡量簡化這個問題,
但是我需要我的handlebar.js'{{input value =“ searchTerm”}}}每次URL更改時將searchTerm的值設置為'null'。
上述輸入旨在自動過濾同一頁面上的列表。 輸入輸入會自動將“ searchTerm”的值更新為鍵入的內容
在此名為searchResults的操作中定義了searchTerm值
這是控制器中的屬性:
searchResults: function() {
var searchTerm = this.get('searchTerm');
var regExp = new RegExp(searchTerm, 'i');
// We use `this.filter` because the contents of `this` is an array of objects
var filteredResults = this.filter(function(category) {
return regExp.test(category.get('categoryName'));
});
return filteredResults;
}.property('@each.categoryName', 'searchTerm'),
因此,我需要分解:
searchTerm
的當前值是什么,都需要將其設置為''。 這可以通過didTransition
或willTransition
事件處理程序在Route上完成。 路由有一個對其controller
的引用(通過this.controller
),您仍然可以通過controllerFor
來獲取它(例如: var c = this.controllerFor('controllerName')
;因此您可以執行以下操作:
App.SomeRouteThatErasesTheSearchTermRoute = Ember.Route.extend({
actions: {
// you could implement with didTransition. No arguments for didTransition tho
didTransition: function() {
this.controller.set('searchTerm', '');
},
// OR, implement with willTransition
willTransition: function(transition) {
this.controller.set('searchTerm', '');
}
}
});
您還可以將其實現為on('willTransition')
觸發的單獨方法,因此您可以保留任何現有處理程序,而只需追加您的處理程序即可。 然后,您可以將其放入一個混合中以供其他路由使用,或者其他路由可以從此類繼承而無需破壞任何其他自定義過渡處理程序(如果有)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.