Constructor
new Component(renderer, htmlNodeId, childTagsopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
renderer |
function | A non-parameter function that returns HTML. | |
htmlNodeId |
String | The id of the HTML element where this Component should render to. | |
childTags |
Array.<String> |
<optional> |
An optional Array of strings of custom-tags for dynamically created child Components (See Component#createChildComponent). |
Examples
<!-- html page -->
<body>
<div id="mycomponent"></div>
</body>
//Javascript
var counter = 1;
var component = new Component(
() => '<div>Counter: <span>'+counter+'</span></div>', // renderer function
'mycomponent' // HTML element id
);
component.start(); // first render
setInterval(() => {
counter++;
component.render(); // component re-render
}, 1000);
Members
childComponentIds :Object.<string, Component>
Type:
- Object.<string, Component>
childComponents :Array.<Component>
Type:
- Array.<Component>
childTags :Array.<String>
During render, if in the HTML provided by the renderer function one of these tags is found, the createChildComponent() function is called.
Type:
- Array.<String>
eventListeners :Object.<string, {callback: function(), eventType: String}>
Type:
- Object.<string, {callback: function(), eventType: String}>
htmlNodeId :String
Type:
- String
stopped :Boolean
Stopped Components do not render.
Type:
- Boolean
Methods
addChildComponent(component)
The HTML element where the child Component will render will not be re-rendered when this Component (the parent) is re-rendered.
The child component will be started (and thus immediately rendered) or stopped if this Component is currently started or stopped, respectively.
Parameters:
Name | Type | Description |
---|---|---|
component |
Component | The child Component. |
addEventListener(eventType, nodesQuery, callback)
Listeners added to elements controlled by this Component should be added via this method, not directly to the HTML elements, because they can be removed during re-render. Listeners added with this method are always restored to the elements matching the selector query after rendering.
Parameters:
Name | Type | Description |
---|---|---|
eventType |
String | The event type to be added to the elements. |
nodesQuery |
String | A HTML selector query to find elements to attach the listener to. |
callback |
function | The callback function to dispatch the event. |
afterRender()
beforeRender()
createChildComponent(className, element, id) → {Component}
Parent components define where to create new children via their rendered HTML in two ways:
- Via
fronty-component
attribute. An element like<div fronty-component="ChildComponent"></div>
indicates thatChildComponent
instances should be created and rendered on that element. - Via custom HTML tag. These tags
must be indicated in the constructor of the component. For example:
new Component(renderer, 'parentId', ['ChildComponent'])
, indicates thatChildComponent
should be created and rendered into when elements with this tag name are found. Note: Remember that custom HTML tags do not work at any place. For example, as child of a<table>
element.
Everytime a new element indicating that a child should be created, this method is called to create the real instance.
Note: By default, this function uses eval(''+className)
to create the instance. If you are packing your application and this library
in different modules, eval may fail in finding the className. You must
override the method to create the child.
Parameters:
Name | Type | Description |
---|---|---|
className |
String | The class name found in the HTML element |
element |
Node | The HTML element where the new child will be placed |
id |
String | The HTML id found in the tag. |
- Source:
- See:
Returns:
- Type
- Component
getChildComponent(id) → {Component}
Parameters:
Name | Type | Description |
---|---|---|
id |
String | The HTML element id. |
Returns:
- Type
- Component
getChildComponents() → {Array.<Component>}
Returns:
- Type
- Array.<Component>
getChildComponentsById() → {Array.<String, Component>}
Returns:
- Type
- Array.<String, Component>
getHtmlNodeId() → {String}
This element will be replaced with the contents of this component renderer function.
Returns:
- Type
- String
onStart()
onStop()
removeChildComponent(component)
After the child removal, this component will re-render.
Parameters:
Name | Type | Description |
---|---|---|
component |
Component | The child Component. |
render()
- Call the renderer function.
- Calculate the differences between the previous "virtual" DOM of this Component and the new "virtual" DOM provided by the renderer function, skipping those elements where child nodes are rendering.
- Patch the previous "virtual" DOM with the previously computed differences, and save it as the next previous "virtual" DOM.
- Patch the real DOM with the previously computed differences.
- Restore the child Components in their new places if they where moved to another part in the DOM.
- Create child nodes if new elements with tag name in
Component#childTags or with
fronty-component
attribute are found in the HTML.
setHtmlNodeId(htmlNodeId)
The element will be replaced with the contents of this component renderer function.
Parameters:
Name | Type | Description |
---|---|---|
htmlNodeId |
String | The HTML node id where this Component will be rendered. |
start()
A Component need to be started in order to render. If this Component was stopped, it will render. Once this Component has been started and rendered, the onStart() hook is called.
stop()
Stopped Components do not render. Once this Component
updateChildComponent()
Type Definitions
renderer() → {String}
Returns:
- Type
- String