mu is too short in accuracy:
this.$el = $(this.el);
And it's easy to see why, look at the view _setElement function :
_setElement: function(el) { this.$el = el instanceof Backbone.$ ? el : Backbone.$(el); this.el = this.$el[0]; },
This ensures that el always a DOM element and that $el always a jQuery object. So, the following is true, although I used the jQuery object as a parameter or property of el :
var myView = new Backbone.View({ el: $('.selector') }); // or var MyView = Backbone.View.extend({ el: $('.selector') });
What is a cached jQuery object?
This is a jQuery object contained within a variable for reuse. This avoids the costly operation of finding an element with something like $(selector) every time.
Here is an example:
render: function() { this.$el.html(this.template(/* ...snip... */)); // this is caching a jQuery object this.$myCachedObject = this.$('.selector'); }, onExampleEvent: function(e) { // avoids $('.selector') here and on any sub-sequent example events. this.$myCachedObject.toggleClass('example'); }
See the extended answer I wrote to find out more.
Emile Bergeron Oct 29 '16 at 18:14 2016-10-29 18:14
source share