http://stackoverflow.com/questions/42067413/how-to-read-data-bind-property-value-from-config-file-and-bind-i-to-view
by jlspake
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div>
<table>
<tbody data-bind="foreach: data">
<tr>
<td>
<input type="text" style="width: 24px;" readonly="readonly" data-bind="value: $index"/>
</td>
<td>
<input type="text" data-bind="applyBindings: $parent.bindingProperties" />
</td>
</tr>
</tbody>
</table>
</div>
JavaScript
var viewModel = function(){
var self = this;
self.bindingProperties = ko.observable();
self.data = ko.observableArray([
{ text: ko.observable('sample text 1'), isVisible:true },
{ text: ko.observable('sample text 2'), isVisible:true },
{ text: ko.observable('sample text 3'), isVisible:false },
{ text: ko.observable('lorem ipsum etc.'), isVisible:true }
]);
/*
//this would load the external bindings file if you're using require.js
require(['dynamicBindings'], function(bindings){
self.bindingProperties(bindings);
})
*/
//simulate delayed loading with a timeout
setTimeout(function(){
self.bindingProperties({ 'value': 'text', 'visible': 'isVisible' });
}, 100);
}
ko.bindingHandlers.applyBindings = {
update: function(element, valueAccessor, allBindingsAccessor, data, bindingContext) {
var bindings = ko.unwrap(valueAccessor()) || {}; //should be an object with multiple properties like: { bindingName: propertyName }
for (var name in bindings) {
if (bindings.hasOwnProperty(name)) {
var binding = {};
var target = data[bindings[name]];
binding[name] = function () { return target; };
ko.applyBindingAccessorsToNode(element, binding, bindingContext);
}
}
}
}
ko.applyBindings(new viewModel());