how to stop durandal / jquery stripping out script tags from views?

前端 未结 3 1514
南旧
南旧 2020-12-19 14:58

I know there are better patterns but in this case there\'s some legacy code that we\'d like to move to a view. This html has a



        
3条回答
  •  感情败类
    2020-12-19 15:38

    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("");
        }
    };
    

    In your view (.html file):

    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.

提交回复
热议问题