Components
Components take the code/markup combo idea and make it reusable. They consist of a Javascript source file (like a controller) and its corresponding Handlebars template.
Unlike controllers, they are isolated by design. They are not aware of their surrounding context, but they receive data through their attributes, including actions (closure functions).
A sample component template could look like this:
Given the above template, you can now use the <BlogPost />
component:
The Component Lifecycle
Part of what makes components so useful is that they let you take complete control of a section of the DOM. This allows for direct DOM manipulation, listening and responding to browser events, and using 3rd party JavaScript libraries in your Ember app.
As components are rendered, re-rendered and finally removed, Ember provides lifecycle hooks that allow you to run code at specific times in a component's life.
On Initial Render
init
didReceiveAttrs
willRender
didInsertElement
didRender
On Re-Render
didUpdateAttrs
didReceiveAttrs
willUpdate
willRender
didUpdate
didRender
On Component Destroy
willDestroyElement
willClearRender
didDestroyElement
Last updated