Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger an action in a text field from a button

Tags:

ember.js

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 this code. Thanks for your help.

// 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>
like image 254
Nicholas Henry Avatar asked Dec 30 '11 17:12

Nicholas Henry


1 Answers

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();
    }
});
like image 175
pangratz Avatar answered Nov 04 '22 12:11

pangratz