Hello.

There is a view:

var SessionView = Backbone.View.extend({ el: $("#main_content"), template: JST["main"], render: function() { $(this.el).html(this.template); return this; } }); var view = new SessionView(); 

And the router:

 var ProfileRouter = Backbone.Router.extend({ routes: { "main": "main" }, main: function(){ view.render(); } }); 

Eventually:
this.el is undefined , and thus the template is not rendered.
But if $ (this.el) .html (this.template); replace with $ ("# main_content"). html (this.template); - then everything works.

Tried to use this. $ El.html () - it is also not rendered, although the selector indicates the correct

Tell me please what am I doing wrong?

  • And in the debugger during the execution of this what does it indicate? Look, maybe you will find the problem - BOPOH
  • I did not find anything indicating an error, here’s a part: $ el: jQuery.fn.jQuery.init [0] context: HTMLDocument selector: "#main_content" proto: Object [0] cid: "view2" el: undefined Template compile fine but el = undefined - Arche
  • the code is running $(document).ready ? just other reasons why $("#main_content") - undefined not visible. besides this.el you already have a jQuery object so using $(this.el) redundant - Specter
  • "the code is executed in $ (document). ready" - yes this.el is undefined But at least this. $ el is a Jquery object still does not work. - Arche
  • if you claim $("#main_content").html(this.template); works, it follows that during the call view.render(); , #main_content exists, but at runtime var view = new SessionView(); - no, because el: $("#main_content") returns undefined. what version of backbone do you use? - Specter

1 answer 1

Roaming and creating Backbone histrory takes place in $ (document) .ready, and new SessionView has been transferred to routing during rendering, but it still remains.

The fact that SessionView does not see the id #main_content is apparently due to the fact that it is connected before rendering html, but if the View is framed $ (document). Ready, then the router simply does not find it.

Moved connection js files to the bottom of the page and this.el - earned. Thank you for your help and guidance in the right direction of reflection.