Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the parent element within a Backbone view?

Tags:

backbone.js

In Backbone model views, it seems $(this.el).parent() doesn't work. What is the best way to select the parent element from within a view?

I am setting el by using tagName: "li" for the view.

like image 559
markwoodall Avatar asked May 12 '11 18:05

markwoodall


1 Answers

By default, Backbone assigns an empty div to your view and you can't access its parent, until it's been placed in the DOM through your render function.

You can use your approach of $(this.el).parent() if you are allowing Backbone to assign the default empty div. You can use the this.el.parent() if you are assigning el yourself in the constructor to a jQuery object.

like image 198
Jake Dempsey Avatar answered Oct 12 '22 08:10

Jake Dempsey