knockout component with custom element styling
by mcannon
HTML
<script src="http://knockoutjs.com/downloads/knockout-3.4.0.js"></script>
<h4>First instance, without parameters</h4>
<message-editor></message-editor>
<h4>Second instance, passing parameters</h4>
<message-editor params="initialText: 'Hello, world!'"></message-editor>
<my-component params="{ data: null }">
<h1>My Data</h1>
<p>Body one</p>
<p>Body two</p>
</my-component>
CSS
message-editor {
font-size: 19px;
}
JavaScript
ko.components.register('message-editor', {
viewModel: function(params) {
this.text = ko.observable(params && params.initialText || '');
},
template: 'Message: <input data-bind="textInput: text" /> '
+ '(length: <span data-bind="text: text().length"></span>)'
});
ko.components.register('my-component', {
viewModel: function(params) {
this.text = ko.observable(params && params.initialText || '');
},
template: '<div class="table-responsive">
<table data-bind="${dataBind}" class="table table-striped table-condensed ${cssClass}" id="${id}" role="${role}">
<jsp:doBody />
</table>
</div>'
});
ko.applyBindings();