Search code examples
ember.jsember-components

Why assigning a property at init and didUpdateAttrs are different?


At Ember.js Guide, there is an example like that:

import Ember from 'ember';

export default Ember.Component.extend({
  init() {
    this._super(...arguments);
    this.errors = [];
  },

  didUpdateAttrs() {
    this._super(...arguments);
    this.set('errors', []);
  },

  ...
});

Why assigning to errors property is different at init and didUpdateAttrs? What are their differences?

UPDATED according to @locks answer:

When is observability set? After init? But parent component's values had been assigned to child component's properties before init.


Solution

  • This has to be mostly with observability. On init, observability hasn't been set up, so this.errors = [] and this.set('errors', []) are functionally similar.

    If you were to do this.errors = [] inside the didUpdateAttrs lifecycle hook, the related computed properties and template wouldn't update. This is because you currently need get/set to be KVO-compliant.