EmberJS - 观察控制器中{{input}} - 助手的变化

时间:2013-12-08 13:58:15

标签: ember.js

我的模板中有一个{{input}} - 帮助器,我想在控制器中为{{input}} - 帮助器中的每个更改触发一个方法。

application.hbs

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

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

有什么建议吗?提前致谢。不要害怕提问。

2 个答案:

答案 0 :(得分:3)

我认为使用actions哈希内部的观察不起作用。您需要在该对象之外提取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
    }
  }
});

答案 1 :(得分:2)

Ember 2有一个很好的解决方案:
{{input key-press="action"}}

请参阅docs