【问题标题】:making computed properties binding aware in Ember在 Ember 中使计算属性绑定感知
【发布时间】:2013-02-13 00:19:48
【问题描述】:

如果我在模板中使用计算属性,我如何让模板知道状态已经以影响计算值的方式发生变化?

例如,如果我显示两个可以递增的数字及其总和,如下所示

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);},
});
<script type="text/x-handlebars">
  <button {{action "incX"}}> {{x}} </button>
  <button {{action "incY"}}> {{y}} </button>
  <p> {{sum}} </p>
</script>

x 和 y 值将在显示中更新,但总和不会更新。我如何告诉车把/余烬总和取决于 x 和 y?

【问题讨论】:

    标签: javascript ember.js handlebars.js


    【解决方案1】:

    计算属性 (.property(_)) 的参数不是您想要访问的,而是用于指定计算属性应观察哪些值并在这些值发生变化时重新计算。

    在你的情况下:

    sum: function() {
        return this.get("x") + this.get("y");
    }
    .property("x", "y")
    

    【讨论】:

    猜你喜欢
    • 2013-06-26
    • 1970-01-01
    • 2014-08-19
    • 2014-09-06
    • 1970-01-01
    • 2014-07-05
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多