Making a computed property dependent on all the properties of another object in emberjs

后端 未结 1 1708
无人及你
无人及你 2021-01-22 03:47

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

html:



        
相关标签:
1条回答
  • 2021-01-22 04:36

    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/

    0 讨论(0)
提交回复
热议问题