简体   繁体   English

kickout.js外部模型js文件

[英]knockout.js external model js file

I have an MVC.NET app which using Knockout.js (+ knockout.mapping) to deal with some cascading dropdowns. 我有一个MVC.NET应用程序,该应用程序使用Knockout.js(+基因敲除.mapping)处理一些级联的下拉菜单。 The data for these comes from a WebAPI call to an external service. 这些数据来自对外部服务的WebAPI调用。 As it happens this service requires an authentication token which expires after 2 hours, so I have the MVC app put the data from the service in a System.Web.Caching.Cache and return it from there unless the token has expired where it will grab it again from the service. 碰巧的是,此服务需要身份验证令牌,该令牌将在2个小时后过期,因此我让MVC应用将来自服务的数据放入System.Web.Caching.Cache中,然后从那里返回数据,除非令牌已过期,否则它将在此进行抓取再次从服务。 This is working fine. 一切正常。 However when I need to get this to the View, I am currently using the following method, which is to have a property of the ViewModel that I assign the service/Cache data to and then do this in the view: 但是,当我需要将其传递给视图时,当前正在使用以下方法,该方法具有ViewModel的属性,该属性将服务/缓存数据分配给该模型,然后在视图中执行此操作:

var model = new ViewModel(@Html.Raw(Json.Encode(Model.ReferenceData)))
ko.applyBindings(model); 

where Model.ReferenceData is the data from the service. 其中Model.ReferenceData是来自服务的数据。 again this is working fine, but... the thing is with this, that the page then has all that Json data dumped in it on each request. 再次可以正常工作,但是...问题就在于此,然后该页面将在每个请求中转储了所有Json数据。 I would like to use an external JS file for the ReferenceData as then at least it can be cached by the browser and lessen the weight of the page on future requests. 我想对ReferenceData使用外部JS文件,因为至少它可以由浏览器缓存,并减轻将来请求时页面的重量。 However, I imagine that the overhead of generating a JS file is not that small, along with – what I really need is it to generate a link to that file that changes in much the same way that the built in MVC bundling of js files works – generating a link with a querystring. 但是,我想生成JS文件的开销不会那么少,而且–我真正需要的是它生成到该文件的链接,该链接的变化方式与内置MVC绑定js文件的方式几乎相同–生成带有查询字符串的链接。 My question is: is there an easy way of doing this? 我的问题是:是否有一种简单的方法? For sure I can, when the cache is filled that first time, generate a js file and reference that from the View, but as I say getting that to change its link each time it is refreshed – or at least working out whether the data in it has changed and updating it only then is where the problem lies. 可以肯定的是,当第一次填充缓存时,我可以生成一个js文件并从View中引用它,但是正如我所说的,每次刷新时都要更改它的链接-或至少要确定是否包含数据它已经改变并只有在问题出现的地方才进行更新。

Any insight to this would be of great help 对此有任何见解都会有很大帮助

Thanks 谢谢

Nat 纳特

  1. Version the JS file (you can keep a GUID in the file it-self). 版本化JS文件(您可以自行将GUID保留在文件中)。
  2. In Application_Start() get this version ID to a static variable. 在Application_Start()中,将此版本ID转换为静态变量。
  3. In your controller pass this static variable data to ViewBag. 在您的控制器中,将此静态变量数据传递给ViewBag。
  4. Ref your script with this ID 使用此ID引用脚本

When you regenerate the file, update the version in file as well as your static variable. 重新生成文件时,请更新文件中的版本以及您的静态变量。 Next request from the client get the new version with new key. 客户端的下一个请求获得具有新密钥的新版本。

Now if you want to update clients on the new version you have to use bi-directional protocol like web sockets or long-polling. 现在,如果要在新版本上更新客户端,则必须使用双向协议,例如Web套接字或长轮询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM