guides icon indicating copy to clipboard operation
guides copied to clipboard

Better docs around `init` lifecycle hook in components

Open rtablada opened this issue 8 years ago • 11 comments

The component lifecycle guide is missing a core hook in terms of init:

https://guides.emberjs.com/v2.14.0/components/the-component-lifecycle/

Since it's an inherited method from Ember.Object, the init function doesn't have docs on this page (even though it is listed as part of the lifecycle. To existing Ember devs this may seem like a no brainer, but there are important details that are different for components:

  • Does the component have access to passed in attribute values
  • Will init be called for every component or just the first instance

Covering these clearly I think will better clarify the difference between using init and didReceiveAttrs for component initialization.

rtablada avatar Jul 25 '17 16:07 rtablada

but there are important details that are different for components

Different how?

locks avatar Jul 25 '17 16:07 locks

@locks see bullets for things that should be clarified for components

rtablada avatar Jul 25 '17 16:07 rtablada

Both of them are the same as objects.

locks avatar Jul 25 '17 16:07 locks

Objects don't have attributes (passed in by HBS templates) and "same as objects" could be said about controllers, but controllers act as singletons so things should be clarified for the use case to be more understandable to newcomers.

rtablada avatar Jul 25 '17 17:07 rtablada

Components don't have attrs either, Ryan. :P

locks avatar Jul 25 '17 17:07 locks

I fixed attrs. Here's an example that is unique to components in a way that should be explained in the lifecycle hooks and can't just be labeled as "it's just like Ember.Object":

https://ember-twiddle.com/bae3061e207b436b4395d67d7534a0c5?openFiles=components.my-component.js%2Ctemplates.components.my-component.hbs

// my-component.js
import Ember from 'ember';

export default Ember.Component.extend({
  init() {
    console.log(arguments);
    // Where is this getting set???
    console.log(this.get('hi'));
    
    this._super(...arguments);
  }
});

rtablada avatar Jul 25 '17 17:07 rtablada

The invoking HBS is:

{{my-component hi="world"}}

The issue is that it is unclear to readers that hi is already set in init. Especially with the naming of didReceiveAtrrs it would make sense to devs that hi would not be available until the didReceiveAtrrs hook runs.

rtablada avatar Jul 25 '17 17:07 rtablada

@rtablada Is having attributes already set in init documented anywhere else? It may not be public api.

Gaurav0 avatar Jan 05 '18 22:01 Gaurav0

@Gaurav0 thanks for pinging me on this one. This PR has gotten way out of date, but is still needed.

Yeah component initialization is like calling MyComponent.create(attrs) in a way.

We shouldn't assume that people reading the component docs know how Ember.Object works.

rtablada avatar Jan 05 '18 23:01 rtablada

@rtablada yinz submitting a PR?

locks avatar Jan 16 '18 13:01 locks

+1

payne-chris-r avatar Apr 10 '18 14:04 payne-chris-r