calling javascript on rendering views in BackBone js. post-render callback?

后端 未结 8 1645
粉色の甜心
粉色の甜心 2020-12-04 19:55

behold, a backbone view render call:

render: function() {
  $(this.el).html(this.template({title: \'test\'}));  //#1
  this.renderScatterChart();
  return th         


        
相关标签:
8条回答
  • 2020-12-04 20:52

    It is because you run render before .el is inserted into the DOM. Check my self explanatory code(run in a blank page with Backbone.js included):

    function someThirdPartyPlugin(id){
       if( $('#' +id).length ===0  ){
        console.log('Plugin crashed');
       }else{
        console.log('Hey no hacks needed!!!'); 
       }
    }
    
    var SomeView = Backbone.View.extend({
        id : 'div1',
        render : function(){
          this.$el.append("<p>Hello third party I'm Backbone</p>");
          someThirdPartyPlugin( this.$el.attr('id') );
          return this;
      }
    }); 
    var SomeView2 = Backbone.View.extend({
        id : 'div2',
        render : function(){
          this.$el.append("<p>Hello third party I'm Backbone</p>");
          someThirdPartyPlugin( this.$el.attr('id') );
          return this;
      }
    }); 
    
    var myView1 = new SomeView();
    $('body').append( myView1.render().el );
    var myView2 = new SomeView2();
    $('body').append( myView2.el );
    myView2.render();
    
    0 讨论(0)
  • 2020-12-04 20:53

    Had the same problem as you did...with highcharts. I was using Backbone.LayoutManager, and ended up hacking the code to add a postRender callback. Would love to see this as a part of Backbone.js

    0 讨论(0)
提交回复
热议问题