But wondering about the case where I want a much simpler interface surface area with more complex internal logic around setting up the default values of my component, that the external world shouldnāt have to think about.
{{ my-component }}
didInsertElement seems perfectly adequate, but wondering if other people are finding that they use a dedicated initialization method, or ever find they need to call super.
Hello, Iām new to Ember, just a month ago I started to develop
thick web client based on Metronic layout. I have a question regarding use of init event. I donāt
see it here in the docs Component - 4.6 - Ember API Documentation , just the
private āinitā method. Could you please explain what is āinitā? I heard on podcast that one
should not rely on private API because it can be deprecated in future versions.
Most of the time you donāt need to implement your own init methods unless you are doing something very special.
I think there are two basic patterns, in most cases I would prefer the āsetupā method that gets called when init is called.
Your own setup method that happens when init event is fired.
setup: Ember.on('init', function() {
// do setup work ...
// function happens whenever init is called
// but you don't have to call super because
// you are not overriding init
// think of this function happening "along side" init
// because of this no need to call super
}),
When we need to override init
init: function() {
// do initialization work...
// this function is called whenever the object is created
// you are overriding an 'init' method that already exists
// in the class hierarchy
// You can think of this function happening "in place of" init
// therefore you need to call super to make sure other init methods
// further up in the hierarchy get called
// call super to initialize other init methods
this._super();
},
Can anyone provide a clue as to whether didInsertElement or init is better? I have just used didInsertElement for the first time and it feels OK.
In my case I needed to check a model that was passed into the component in order to set the value of a simple flag. Specifically, if the model has no label attribute set then I set the editable flag to true. This allows users to create unlabelled objects via drag/drop and add a label when they see it rendered. For previously labelled objects, they are not editable by default (but the editable flag can be toggled with a click).