2

我希望exampleFunction每次“exampleProperty”更改时都调用该函数。我应该如何设置?似乎这种情况最近发生了变化。我正在使用 1.0.0-RC1。

模板:

   <div id="container">
      {{view Ember.TextField valueBinding="exampleProperty"}}
    </div>

控制器:

ExampleApp.ApplicationController = Ember.Controller.extend({

  exampleProperty: "example",

  exampleFunction: function() {
    console.log("exampleProperty was changed");
  }

});
4

2 回答 2

3

您将 exampleFunction 定义为观察者。最直接的语法是使用 'observes' 方法Function

exampleFunction: function () {
    console.log("property changed");
}.observes('exampleProperty')

您可以在 Ember 指南中阅读有关观察者(以及更多内容)的更多信息:http: //emberjs.com/guides/object-model/observers/

于 2013-03-20T16:14:36.313 回答
2

您将创建一个“观察”属性。

ExampleApp.ApplicationController = Ember.Controller.extend({

  exampleProperty: "example",

  exampleFunction: function() {
    console.log("exampleProperty was changed");
  }.observes("exampleProperty")

});
于 2013-03-20T16:15:04.923 回答