简体   繁体   English

EmberJS-观察控制器中{{input}}-Helper中的更改

[英]EmberJS - Observing changes in an {{input}}-Helper in a controller

i have a {{input}}-helper in my template and i want to fire a method in the controller for every change in the {{input}}-helper. 我的模板中有一个{{input}}-helper,我想为{{input}}-helper中的每个更改在控制器中激发一个方法。

application.hbs application.hbs

<div class="page" id="main">
    {{input type="text" id="note-title" value=noteTitle action="createNote"}}
</div>

application_controller.js application_controller.js

YeoApp.ApplicationController = Ember.ArrayController.extend({
    actions: {
        searchTextChanged: function() {
            // this method should be called whenever
            // the input value changes, but somehow it doesn't work

        }.observes("noteTitle"),
        anotherMethod: function() {
            this.set("noteTitle", "Test!");
            //even this doesn't fire the observer when called
        }
    }
});

Any suggestions? 有什么建议么? Thanks in advance. 提前致谢。 Don't be afraid to ask questions. 不要害怕问问题。

I think that using observes inside of the actions hash don't work. 我认为在actions哈希内部使用观察不起作用。 You need to extract the searchTextChanged outside of that object: 您需要将searchTextChanged提取到该对象之外:

YeoApp.ApplicationController = Ember.ArrayController.extend({
  searchTextChanged: function() {
    // this method should be called whenever
    // the input value changes, but somehow it doesn't work
  }.observes("noteTitle"),
  actions: {        
    anotherMethod: function() {
      this.set("noteTitle", "Test!");
      //even this doesn't fire the observer when called
    }
  }
});

Ember 2 has a nice solution: Ember 2有一个不错的解决方案:
{{input key-press="action"}}

See the docs 查看文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM