从按钮的文本字段中触发动作 [英] Trigger an action in a text field from a button

查看:122
本文介绍了从按钮的文本字段中触发动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何从按钮触发此视图函数insertNewLine的建议(请参阅下面的视图和模板)。我猜想可能是一个更好的方法来构建这个代码。感谢您的帮助。

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>


推荐答案

您可以使用mixin 当调用 insertNewline 时,TextField上的Ember.TargetActionSupport 并执行 triggerAction()。请参阅 http://jsfiddle.net/pangratz666/zc9AA/

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

Handlebars:

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:

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();
    }
});

这篇关于从按钮的文本字段中触发动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆