[英]In Ember How Should I Call a Function in the Controller Whenever A Property Changes
I would like the function exampleFunction
to be called every time `exampleProperty' changes. 我希望每次`exampleProperty'更改时都调用函数exampleFunction
。 How should I set that up? 我应该如何设置? Seems this has changed recently. 似乎最近情况有所改变。 I'm using 1.0.0-RC1. 我正在使用1.0.0-RC1。
The template: 模板:
<div id="container">
{{view Ember.TextField valueBinding="exampleProperty"}}
</div>
The controller: 控制器:
ExampleApp.ApplicationController = Ember.Controller.extend({
exampleProperty: "example",
exampleFunction: function() {
console.log("exampleProperty was changed");
}
});
You would define exampleFunction as an observer. 您可以将exampleFunction定义为观察者。 The most straightforward syntax is to use the 'observes' method on Function
as such: 最直接的语法是在Function
上使用“ observes”方法,如下所示:
exampleFunction: function () {
console.log("property changed");
}.observes('exampleProperty')
You can read more about observers (and more) at the Ember guide: http://emberjs.com/guides/object-model/observers/ 您可以在Ember指南中阅读有关观察者的更多信息(以及更多信息): http : //emberjs.com/guides/object-model/observers/
You would create a "observes" property. 您将创建一个“观察”属性。
ExampleApp.ApplicationController = Ember.Controller.extend({
exampleProperty: "example",
exampleFunction: function() {
console.log("exampleProperty was changed");
}.observes("exampleProperty")
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.