使计算属性依赖于 emberjs 中另一个对象的所有属性 [英] Making a computed property dependent on all the properties of another object in emberjs

查看:25
本文介绍了使计算属性依赖于 emberjs 中另一个对象的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使一个属性依赖于另一个对象的所有属性?例如:(也在 jsfiddle):

Is it possible to make a property dependent on all the properties of another object? For example: (also in jsfiddle):

html:

<script type="text/x-handlebars" >
    {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="index">
    <p>Weapon damage: {{ input value=model.activeWeapon.damage}} </p>
    <p>Hero damage: {{ model.weaponDamagePerSecond }}</p>
</script>

javascript:

javascript:

App = Ember.Application.create({});

App.Weapon = Ember.Object.extend({
    damage: null,
    speed: null
});

App.Hero = Ember.Object.extend({
    activeWeapon: null,
    strength: null,
    weaponDamagePerSecond: function() {
      var weapon = this.get('activeWeapon');
      console.log('weapon damage:' + weapon.get('damage'));
      console.log('weapon speed:' + weapon.get('speed'));
      console.log('strength:' + this.get('strength'));
      console.log (weapon.get('damage') + this.get('strength')) * weapon.get('speed');

      return (1.0 * weapon.get('damage') + 1.0 * this.get('strength')) * weapon.get('speed');
    }.property('activeWeapon', 'strength')
    //}.property('activeWeapon.damage', 'activeWeapon.speed', 'strength')
    // the above line will make the example work, but it is a bit painful to 
    // have to specify all the attributes needed of activeWeapon.
});

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return App.Hero.create({
            activeWeapon: App.Weapon.create({
                damage: 40,
                speed: 2
            }),
            strength: 8
        });
    }
});

是否可以让 weaponDamagePerSecondactiveWeapon 上的任何属性更新时更新,而必须手动指定所有属性(例如在注释掉的代码中)?

Is it possible to get weaponDamagePerSecond to update when any property on activeWeapon updates, with having to specify all the properties manually (like in the commented out code)?

推荐答案

不,但是编写一个观察者真的很容易,它可以监视所有内容并在它更新时触发您的计算属性进行更新.我不一定推荐这个,但可以做到.

No, but it's really easy to write up an observer that watches everything and triggers your computed property to update anytime it updates. I'm not necessarily recommending this, but it can be done.

http://jsfiddle.net/5gS59/

App.Weapon = Ember.Object.extend({
    damage: null,
    speed: null,
    version: 0,
    watchingEverything: function(){
        console.log('hello');
        this.incrementProperty('version');
    }.observes('damage','speed')
});


weaponDamagePerSecond: function() {
  var weapon = this.get('activeWeapon');
  console.log('weapon damage:' + weapon.get('damage'));
  console.log('weapon speed:' + weapon.get('speed'));
  console.log('strength:' + this.get('strength'));
  console.log (weapon.get('damage') + this.get('strength')) * weapon.get('speed');

  return (1.0 * weapon.get('damage') + 1.0 * this.get('strength')) * weapon.get('speed');
}.property('activeWeapon.version', 'strength')

你也可以只用一个属性而不是一个观察来做类似的事情:

You can alternately do a similar thing with just a property and not an observes:

http://jsfiddle.net/XsuxA/1/

这篇关于使计算属性依赖于 emberjs 中另一个对象的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆