简体   繁体   English

避免从骨干视图中重新渲染图像和其他内容

[英]Avoid re-rendering images and other stuff from backbone views

when I rerender a backbone view, what is a good way to skip re-rendering things like images and google maps? 当我重新渲染骨干视图时,有什么方法可以跳过重新渲染像图像和谷歌地图这样的东西? My photo and map views tend to flicker really bad every time the view is re-rendered (which is quite often). 每次重新渲染视图时,我的照片和地图视图都会变得非常糟糕(这种情况经常发生)。 With the images especially, it is the case that the templating engine is laying out the layout from scratch, which causes the image tags to fetch the bitmap either from the server or from the cache once again. 特别是图像,模板引擎从头开始布局布局,这使得图像标签再次从服务器或缓存中获取位图。

Of course, I still want the view to remain kind of agnostic of the layout, so technically it shouldn't know that we are going to display an image, right? 当然,我仍然希望视图对布局保持一种不可知性,所以从技术上来说它不应该知道我们要显示图像,对吧?

I'm gonna offer a solution that is in conflict with your assumption "the View should be agnostic of the template" . 我将提供一个与您的假设相冲突的解决方案“View应该与模板无关”

If you call render() any time anything has changed in the Model you will have this blinking in your browser, especially if the template is big. 如果你在模型中发生任何变化时调用render() ,你的浏览器就会闪烁 ,特别是如果模板很大的话。

My recommendation is separate the render of the View which happens only once when the View is first time visualized and several update helper methods which are in charge of updating small pieces of the View linked to concrete Model attributes. 我的建议是将View的render分开,它只在View第一次可视化时发生一次,而几个update辅助方法负责更新链接到具体Model属性的View的小部分。

For example: 例如:

// code simplified and not tested
var MyView = Backbone.View.extend({
  initialize: function(){
    this.model.on( "change:title", this.updateTitle, this );
    this.model.on( "change:description", this.updateDescription, this );
    // ... more change:XXX
  },

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
  },

  updateTitle: function(){
    this.$el.find( ".title" ).html( this.model.get( "title" ) );
  },

  updateDescription: function(){
    this.$el.find( ".description" ).html( this.model.get( "description" ) );
  },

  // ... more updateXXX()
})

为了获得最佳结果,您实际上不希望重新呈现包含媒体的HTML,因此我建议对更改的内容使用更多目标视图,这样您就不需要使用不包含内容的内容重新呈现视图“T。

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

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