Avoid re-rendering images and other stuff from backbone views

后端 未结 2 1885
梦谈多话
梦谈多话 2021-02-10 10:13

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 vi

2条回答
  •  醉酒成梦
    2021-02-10 10:20

    I'm gonna offer a solution that is in conflict with your assumption "the View should be agnostic of the template".

    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.

    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.

    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()
    })
    

提交回复
热议问题