javascript - vuejs: Update computed property when nested data changes - Stack Overflow

I have a puted property which relies on a data object updating to perform a calculation and return the

I have a puted property which relies on a data object updating to perform a calculation and return the value. The puted property updates whenever the first level of nesting is updated, however the puted property does not update when nested data props of the base object are updated.

I have provided the code samples below. Essentially the problem occurs when the nested properties of this.form.business_details.usage are updated. To elaborate, when this.form.business_details.quote_type updates, thispletion.business_details.quote_type also updates as the latter is dependent on the former.

However when this.form.business_details.usage.spend_type updates, the puted prop for pletion() is not re-evaluated. Do puted properties not watch nested data by default? If not, how can I trigger an update?

<script>
import Form from '@/classes/Form';
import { filter, map, snakeCase } from 'lodash';

export default {
  name: 'Form',

  puted: {
    pletion() {
      return {
        business_details: {
          postcode: this.generateFormCompletionObject(true, this.form.business_details.postcode),
          quote_type: this.generateFormCompletionObject(true, this.form.business_details.quote_type),
          current_supplier: this.generateFormCompletionObject(true, this.form.business_details.current_supplier),
          knows_usage: this.generateFormCompletionObject(true, this.form.business_details.knows_usage, true),
          usage: {
            spend_type: this.generateFormCompletionObject(
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.knows_usage,
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.spend_type
            ),
            billing_period: this.generateFormCompletionObject(
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.knows_usage,
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.billing_period
            ),
            amount: this.generateFormCompletionObject(
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.knows_usage,
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.amount
            ),
          },
        },
      };
    },

    pletionPercentage() {      
      const required = this.recursiveCompletionCheck(Object.values(thispletion.business_details))
        .flat()
        .filter(value => value)
        .map(value => +!! value.given);

      return required.filter(value => value).length / required.length * 100;
    },
  },

  data() {
    return {
      pleted: {
        business_details: false,
        personal_details: false,
      },
      form: new Form({
        business_details: {
          postcode: new URLSearchParams(window.location.search).get('postcode') || null,
          quote_type: null,
          current_supplier: null,
          knows_usage: null,
          usage: {
            spend_type: null,
            billing_period: null,
            amount: null,
          },
        },
        personal_details: {
          full_name: null,
          email: null,
          phone: null,
          email_consent: false,
        },
      }),
    };
  },

  methods: {
    generateFormCompletionObject(required, key, requiresBoolean = false) {
      return {
        required: required,
        given: requiresBoolean
          ? typeof key === 'boolean' 
          : !! key,
      };    
    },

    recursiveCompletionCheck(values) {
      return values.map(value => {
        if ('required' in value && value.required) return value;
        if ('required' in value && !value.required) return null;

        if (!! Object.keys(value).length) {
          return recursiveCompletionChecker(Object.values(value));
        }

        return null;
      });
    },
  },
}
</script>

I have a puted property which relies on a data object updating to perform a calculation and return the value. The puted property updates whenever the first level of nesting is updated, however the puted property does not update when nested data props of the base object are updated.

I have provided the code samples below. Essentially the problem occurs when the nested properties of this.form.business_details.usage are updated. To elaborate, when this.form.business_details.quote_type updates, this.pletion.business_details.quote_type also updates as the latter is dependent on the former.

However when this.form.business_details.usage.spend_type updates, the puted prop for pletion() is not re-evaluated. Do puted properties not watch nested data by default? If not, how can I trigger an update?

<script>
import Form from '@/classes/Form';
import { filter, map, snakeCase } from 'lodash';

export default {
  name: 'Form',

  puted: {
    pletion() {
      return {
        business_details: {
          postcode: this.generateFormCompletionObject(true, this.form.business_details.postcode),
          quote_type: this.generateFormCompletionObject(true, this.form.business_details.quote_type),
          current_supplier: this.generateFormCompletionObject(true, this.form.business_details.current_supplier),
          knows_usage: this.generateFormCompletionObject(true, this.form.business_details.knows_usage, true),
          usage: {
            spend_type: this.generateFormCompletionObject(
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.knows_usage,
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.spend_type
            ),
            billing_period: this.generateFormCompletionObject(
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.knows_usage,
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.billing_period
            ),
            amount: this.generateFormCompletionObject(
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.knows_usage,
              typeof this.form.business_details.knows_usage === 'boolean' && this.form.business_details.amount
            ),
          },
        },
      };
    },

    pletionPercentage() {      
      const required = this.recursiveCompletionCheck(Object.values(this.pletion.business_details))
        .flat()
        .filter(value => value)
        .map(value => +!! value.given);

      return required.filter(value => value).length / required.length * 100;
    },
  },

  data() {
    return {
      pleted: {
        business_details: false,
        personal_details: false,
      },
      form: new Form({
        business_details: {
          postcode: new URLSearchParams(window.location.search).get('postcode') || null,
          quote_type: null,
          current_supplier: null,
          knows_usage: null,
          usage: {
            spend_type: null,
            billing_period: null,
            amount: null,
          },
        },
        personal_details: {
          full_name: null,
          email: null,
          phone: null,
          email_consent: false,
        },
      }),
    };
  },

  methods: {
    generateFormCompletionObject(required, key, requiresBoolean = false) {
      return {
        required: required,
        given: requiresBoolean
          ? typeof key === 'boolean' 
          : !! key,
      };    
    },

    recursiveCompletionCheck(values) {
      return values.map(value => {
        if ('required' in value && value.required) return value;
        if ('required' in value && !value.required) return null;

        if (!! Object.keys(value).length) {
          return recursiveCompletionChecker(Object.values(value));
        }

        return null;
      });
    },
  },
}
</script>
Share Improve this question asked May 22, 2020 at 17:56 ThorntonStuartThorntonStuart 1394 silver badges12 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Nested object properties aren't automatically re-evaluated. You can watch for its changes with watch. Here is the documentation for more info. The relevant part you need is:

var vm = new Vue({
  el: '#app',
  puted: {
    foo() { return this.item.foo; }
  },
  watch: {
    foo() { console.log('Foo Changed!'); }
  },
  data: {
    item: { foo: 'foo' }
  }
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745457165a4628540.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信