简体   繁体   English

秘银避免重新加载图像

[英]mithril avoiding to reload image

I am using mithril 0.2.2-rc.1. 我正在使用秘银0.2.2-rc.1。 I saw in the routing documentation : Routing is a system that allows creating Single-Page-Applications (SPA), ie applications that can go from one page to another without causing a full browser refresh. 我在路由文档中看到: 路由是一个系统,它允许创建单页应用程序(SPA),即可以在导致浏览器完全刷新的情况下从一个页面转到另一个页面的应用程序。

Indeed when I am routing to the same page with different parameter only the part that I want to change is refresh expect this : 事实上,当我路由到不同的参数,只是我想更改刷新预计这一部分在同一个页面:

m("img[src='assets/images/logo.png'][alt=''][width='100']")

I can see in the network communication that the image is reloaded (another GET request). 我可以在网络通信中看到图像已重新加载(另一个GET请求)。

Is there a way to avoid that? 有办法避免这种情况吗?

route.js route.js

m.route.mode = "pathname";

m.route(document.getElementById('app'), '/', {
    '/': main,

    '/modelling/:level': main

})

It's difficult to see how the two pieces of code fit together, but two things suggest themselves: 很难看到这两段代码是如何组合在一起的,但是有两点说明了自己:

  1. Every change of route (even if that change results in the same route entry, eg /modelling/x to /modelling/y ) will result in the entire DOM being regenerated. 路由的每次更改(即使该更改导致相同的路由条目,例如/modelling/x/modelling/y )也将导致重新生成整个DOM。 You can prevent this behaviour by calling m.redraw.strategy( 'diff' ) in each route component's controller. 您可以通过在每个路由组件的控制器中调用m.redraw.strategy( 'diff' )来防止此行为。
  2. Repeatedly requesting the same resource does not lead to extra calls to the server: a multi page site with every page requesting the same JS and CSS will only load those resources once, and will hit browser cache on subsequent requests. 重复请求相同的资源不会导致对服务器的额外调用:一个多页面站点,每个页面都请求相同的JS和CSS,只会加载这些资源一次,并且会在后续请求中访问浏览器缓存。 Thus repeatedly asking for the same image resource will not generate any new calls to the server. 因此,反复请求相同的图像资源不会对服务器产生任何新的调用。

If you check the documentation of the m method you will see that the config attribute lets you retain elements across redraws. 如果查看m方法的文档,您将看到config属性可让您在重绘之间保留元素。 So this should work for you: 因此,这应该为您工作:

m('img', {config: function persist(el, isInit, context)}) {
    context.retain = true;
}

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

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