You could use a custom KnockOut binding for this.
Here's a really simple binding - keep in mind this is just an idea:
ko.bindingHandlers.script = {
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var scriptName = ko.utils.unwrapObservable(valueAccessor());
$(element).html("<script src='" + scriptName + "'></script>");
}
};
In your view (.html file):
<div data-bind="script:'dobinding.js'"></div>
I tested this, and I found that the script is indeed injected, parsed, and executed, but it doesn't stay loaded in the DOM. That may or may not be an issue for you.
Seems that stripping the tags may be a Durandal-specific thing. I haven't figured out the cause yet, but I'll update if I get a chance to dig into it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…