我正在寻找有关如何通过按钮触发此 View 函数insertNewLine的建议(请参见下面的 View 和模板)。我猜想可能有更好的方法来构造此代码。谢谢你的帮助。
// view
App.SearchView = Ember.TextField.extend({
insertNewline: function() {
var value = this.get('value');
if (value) {
App.productsController.search(value);
}
}
});
// template
<script type="text/x-handlebars">
{{view App.SearchView placeholder="search"}}
<button id="search-button" class="btn primary">Search</button>
</script>
最佳答案
您可以在TextField上使用mixin Ember.TargetActionSupport
,并在调用triggerAction()
时执行insertNewline
。参见http://jsfiddle.net/pangratz666/zc9AA/
车把
<script type="text/x-handlebars">
{{view App.SearchView placeholder="search" target="App.searchController" action="search"}}
{{#view Ember.Button target="App.searchController" action="search" }}
Search
{{/view}}
</script>
JavaScript:
App = Ember.Application.create({});
App.searchController = Ember.Object.create({
searchText: '',
search: function(){
console.log('search for %@'.fmt( this.get('searchText') ));
}
});
App.SearchView = Ember.TextField.extend(Ember.TargetActionSupport, {
valueBinding: 'App.searchController.searchText',
insertNewline: function() {
this.triggerAction();
}
});