How to stop Knockout 3.2 library loading twice

别说谁变了你拦得住时间么 提交于 2019-12-23 02:28:11

问题


I have the issue that binding expressions such as

<div data-bind="text: $data.Property"></div>

-where Property is an observable- causes the actual text of knockout's observable function to display instead of the value Property is supposed to represent. This was addressed here In IE8, KnockoutJS 3.2 displaying actual observable function rather than the observable's value.

The cause of that issue was that duplicate knockout library files were being loaded. The "UPDATE:" section and answer of the linked SO question includes some detail around that.

I now need to know how to keep the knockout library from being loaded twice. Emphasis on loaded not just executed. So far I haven't found anything that quite answered this.

RequireJS: is used by the site but, not by the pages under investigation.

SignalR: is used by the page. I'm a bit unfamiliar with SignalR so I can't say how likely it is that this is causing multiple loads.

Ajax: is used as well but it is used to receive JSON data.

There is only 1 explicit reference to the knockout library.

Looking at the network tab, the first file is loaded from the speculative download feature of IE. The second file is from the main parser. The first file is completely downloaded, and then the second is completely downloaded.

What I haven't been able to figure out is a way to keep:

  • The Lookahead Downloader from downloading the file the first time
  • The main parser from downloading the file a second time
  • The file from being executed a second time without changing the code in the library file.

One of the above solutions can be acceptable at this point. Can anyone offer insight, suggestions or know of a solution to this?


回答1:


I finally got this figured out. The issue was the order that the scripts were listed in the page. ASP.NET MVC 5 supports renderable named sections. In this case scripts:

Layout -

@RenderSection("scripts", required: false)

Views/Partial Views -

@section scripts {
    // script includes ...
}

In my case a partial view did not include scripts in a scripts section causing them to be parsed/downloaded by the preparser and the main parser.

Adding all scripts to scripts sections in the proper order solved this problem. The order has been determined by the the scripts' dependencies.



来源:https://stackoverflow.com/questions/29854235/how-to-stop-knockout-3-2-library-loading-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!