如何停止durandal/jquery从视图中剥离脚本标签? [英] how to stop durandal / jquery stripping out script tags from views?
问题描述
我知道有更好的模式,但是在这种情况下,有一些我们希望移至视图的旧代码.该html带有
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
<script src="binding.js"></script>
需要在页面呈现和显示后运行.
that needs to run after the page renders and displays.
我希望使用<script src="">
标签来执行此操作,但是该标签似乎已被剥离.
I hoped to do this with a <script src="">
tag, but the tag seems to be getting stripped out.
我们如何获取脚本标记以进行渲染,或者如何解决durandal?
How do we either get the script tag to render, or do a work-around with durandal?
示例html:
<div>
<link rel="stylesheet" href="/Content/stylesheet1.css"/>
<link rel="stylesheet" href="/Content/stylesheet2.css"/>
<header>
<h1>heading</h1>
<h2>sub heading</h2>
</header>
<section>
</section>
<footer>
</footer>
<script src="dobinding.js"></script>
</div>
这很有帮助:
http://knockoutjs.com/documentation/custom-bindings.html
推荐答案
您可以为此使用自定义的KnockOut绑定.
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>");
}
};
您认为(.html文件):
In your view (.html file):
<div data-bind="script:'dobinding.js'"></div>
我对此进行了测试,发现该脚本确实已注入,解析和执行,但它不会一直加载在DOM中.对您而言,这可能是问题,也可能不是问题.
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.
似乎剥离标签可能是Durandal特有的事情.我还没有找到原因,但是如果有机会深入研究,我会进行更新.
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.
这篇关于如何停止durandal/jquery从视图中剥离脚本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!