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

后端 未结 8 1650
粉色の甜心
粉色の甜心 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("

    Hello third party I'm Backbone

    "); someThirdPartyPlugin( this.$el.attr('id') ); return this; } }); var SomeView2 = Backbone.View.extend({ id : 'div2', render : function(){ this.$el.append("

    Hello third party I'm Backbone

    "); 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();

提交回复
热议问题