What’s the difference between:
$(this.el).html
and
this.$el.html
Reading a few backbone examples and some do it one way and other another way.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
$(this.el)wraps an element with jQuery (or Zepto). So, if your view HTML was this:<div id="myViewElement"></div>…and
this.elreferenced that div, then$(this.el)would be the equivalent of retrieving it directly via jQuery:$('#myViewElement').this.$elis a cached reference to the jQuery (or Zepto) object, so a copy of what you would get from calling$(this.el). The intent is to save you the need to call$(this.el), which may have some overhead and therefor performance concerns.Please note: the two are NOT equivalent.
this.elalone is a reference to a host object HTMLElement — no libraries involved. This is the return ofdocument.getElementById.$(this.el)creates a new instance of the jQuery/Zepto object.this.$elreferences a single instance of the former object. It is not “wrong” to use any of them, as long as you understand the costs of multiple calls to$(this.el).In code:
Also, it is worth mentioning that jQuery and Zepto have partial internal caches, so extra calls to
$(this.el)might end up returning a cached result anyway, and that’s why I say “may have performance concerns”. It also may not.Documentation
view.$el– http://backbonejs.org/#View-$el$in backbone – http://backbonejs.org/#View-dollar