Constructor
new ModelComponent(modelRenderer, model, htmlNodeId, childTagsopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
modelRenderer |
function | A renderer function which accepts a Model as argument. | |
model |
Model | The default model. You can add more models with ModelComponent#addModel. | |
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. |
Examples
<!-- html page -->
<body>
<div id="mycomponent"></div>
</body>
// Javascript
// Model
var model = new Model();
model.counter = 0;
// The ModelComponent to render the Model
var component = new ModelComponent(
(m) => '<div>Counter: <span>'+m.counter+'</span></div>', // renderer function
model, //the model
'mycomponent' // HTML element id
);
component.start(); // first render
// Make changes in Model to fire re-renders
setInterval(() => {
model.set( () => model.counter++); // model update -> automatic re-render!
}, 1000);
Extends
Members
childComponentIds :Object.<string, Component>
Type:
- Object.<string, Component>
- Inherited From:
- Source:
childComponents :Array.<Component>
Type:
- Array.<Component>
- Inherited From:
- Source:
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>
- Inherited From:
- Default Value:
- empty array
- Source:
eventListeners :Object.<string, {callback: function(), eventType: String}>
Type:
- Object.<string, {callback: function(), eventType: String}>
- Inherited From:
- Source:
htmlNodeId :String
Type:
- String
- Inherited From:
- Default Value:
- null
- Source:
stopped :Boolean
Stopped Components do not render.
Type:
- Boolean
- Inherited From:
- Default Value:
- true
- Source:
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. |
- Inherited From:
- Source:
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. |
- Inherited From:
- Source:
addModel(modelName, model)
ModelComponents can have more than one model. The model passed in the constructor is the 'default' model. Additional models must have a name. When the modelRenderer function is called, the passed object to the function will contain the 'default' model itself and all the additional models under their respective names. For example, a code like:
var myModel = new Fronty.Model();
myModel.value = 'foo';
var mySecondaryModel = new Fronty.Model();
mySecondaryModel.value = 'bar';
var myModelComponent = new Fronty.ModelComponent(..., myModel, ...);
myModelComponent.addModel('secondary', mySecondaryModel);
will pass the following object to the model renderer function:
{
value: 'foo',
secondary: {
value: 'bar'
}
}
Parameters:
Name | Type | Description |
---|---|---|
modelName |
String | The name for the additional model |
model |
Model | The additonal model |
afterRender()
- Inherited From:
- Source:
beforeRender()
- Inherited From:
- Source:
createChildComponent(tagName, childTagElement, id) → {Component}
The model attribute is used as a path inside the model object and calls ModelComponent#createChildModelComponent.
Parameters:
Name | Type | Description |
---|---|---|
tagName |
String | The HTML tag name used to place the new child Component in the parent HTML |
childTagElement |
Node | The HTML element where the new Child will be placed |
id |
String | The HTML id found in the tag. |
- Overrides:
- Source:
- See:
Returns:
- Type
- Component
Example
<!-- How to add a model attribute in the HTML child tag -->
<childcomponent id="child-0" model="items[0]">
createChildModelComponent(className, element, id, modelItem) → {Component}
If you have components with different constructors or this policy does not adapt to your needs, you can override this method.
Parameters:
Name | Type | Description |
---|---|---|
className |
String | The class name found in the element |
element |
Node | The HTML element where the new child will be placed |
id |
String | The HTML id found in the element. |
modelItem |
Object | a model object for the new Component. |
Returns:
- Type
- Component
getChildComponent(id) → {Component}
Parameters:
Name | Type | Description |
---|---|---|
id |
String | The HTML element id. |
- Inherited From:
- Source:
Returns:
- Type
- Component
getChildComponents() → {Array.<Component>}
- Inherited From:
- Source:
Returns:
- Type
- Array.<Component>
getChildComponentsById() → {Array.<String, Component>}
- Inherited From:
- Source:
Returns:
- Type
- Array.<String, Component>
getHtmlNodeId() → {String}
This element will be replaced with the contents of this component renderer function.
- Inherited From:
- Source:
Returns:
- Type
- String
onStart()
- Inherited From:
- Source:
onStop()
- Inherited From:
- Source:
removeChildComponent(component)
After the child removal, this component will re-render.
Parameters:
Name | Type | Description |
---|---|---|
component |
Component | The child Component. |
- Inherited From:
- Source:
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.
- Inherited From:
- Source:
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. |
- Inherited From:
- Source:
setModel(model)
The component will be re-rendered
Parameters:
Name | Type | Description |
---|---|---|
model |
Model | Array.<Model> | The model(s) to be set. |
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.
- Overrides:
- Source:
stop()
Stopped Components do not render. Once this Component
- Overrides:
- Source:
update(model)
This function simply calls render, but you can override it.
Parameters:
Name | Type | Description |
---|---|---|
model |
Model | The model that has been updated. |
updateChildComponent()
- Overrides:
- Source:
Type Definitions
renderer() → {String}
- Inherited From:
- Default Value:
- null
- Source:
Returns:
- Type
- String