Bos*_*ohn 2 javascript handlebars.js ember.js
如果我在模板中使用计算属性,如何让模板知道状态已经以影响该计算值的方式发生了变化?
例如,如果我显示两个可以递增的数字及其总和,如下所示
App.ApplicationController = Ember.Controller.extend({
x:0,
y:0,
sum: function(){return this.get("x") + this.get("y");}.property("content.sum"),
incX: function(){ this.set("x", this.x+1);},
incY: function(){ this.set("y", this.y+1);},
});
Run Code Online (Sandbox Code Playgroud)
<script type="text/x-handlebars">
<button {{action "incX"}}> {{x}} </button>
<button {{action "incY"}}> {{y}} </button>
<p> {{sum}} </p>
</script>
Run Code Online (Sandbox Code Playgroud)
x和y值将在显示中更新,但总和不会更新.如何判断把手/余烬的总和是否依赖于x和y?
计算属性(.property(_))的参数不是您想要访问的参数,而是指定计算属性应该观察哪些值,并在任何这些值更改时重新计算.
在你的情况下:
sum: function() {
return this.get("x") + this.get("y");
}
.property("x", "y")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
857 次 |
| 最近记录: |