I'm new to knockout template engine and I've run into an issue.
I have a container that contains text input with clickable hints. My aim is to change input text with link caption after it is clicked by user.
html code snippet:
<script type="text/html" id="query-input-template">
<input data-bind="value: query" type="search" placeholder="query"></input>
<span>
Example: <span data-bind="template: { name: 'hint-template', data: { hint: hints[0] }}"></span>
or <span data-bind="template: { name: 'hint-template', data: { hint: hints[1] }}"></span>
</span>
</script>
<script type="text/html" id="hint-template">
<a href="#" data-bind="text: hint, click: vm.query.bind($data, hint)"></a>
</script>
<div id="search-container" data-bind="template: { name: 'query-input-template' }"></div>
js code snippet:
viewModel = function() {
var self = this;
self.hints = [ "Test query 1", "Test query 2" ];
self.query = ko.observable();
}
var vm = new viewModel();
ko.applyBindings(vm, document.getElementById('search-container'));
codepen version: https://codepen.io/YungJ/pen/VjpEaq?editors=1010
This code works properly and I'm quite satisfied with the result. But I'm sure that it can be rewritten appropriately. The thing I'm not satisfied with the second template's "vm.query" binding. I want to pass view model from upper template to get rid of this and recieve more clean query.bind($data, hint)
binding