Trigger an action in a text field from a button

后端 未结 1 895
暗喜
暗喜 2021-02-09 04:14

I\'m looking for advice on how to trigger this view function insertNewLine from a button (see view and template below). I\'m guessing there\'s probably a better way to structure

相关标签:
1条回答
  • 2021-02-09 04:51

    You could use the mixin Ember.TargetActionSupport on your TextField and execute triggerAction() when insertNewline is invoked. See http://jsfiddle.net/pangratz666/zc9AA/

    Handlebars:

    <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();
        }
    });
    
    0 讨论(0)
提交回复
热议问题