Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $el and el in Backbone.js views?

Can you please tell the difference between $el and el in Backbone.js views?

like image 914
ali asad Avatar asked May 20 '13 09:05

ali asad


People also ask

What is El in Backbone JS?

el() The Backbone. js View el method defines the element that is used as the view reference. this. el is created from the view's tagName, className, id and attributes properties, if specified.

How does Backbone JS work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

Is backbone a JavaScript framework?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.


2 Answers

mu is too short is exactly right:

this.$el = $(this.el); 

And it's easy to understand why, look at the view's _setElement function:

_setElement: function(el) {   this.$el = el instanceof Backbone.$ ? el : Backbone.$(el);   this.el = this.$el[0]; }, 

This ensures that the el property is always a DOM element, and that the $el property is always a jQuery object wrapping it. So the following is valid even though I used a jQuery object as the el option or property:

// Passing a jQuery object as the `el` option. var myView = new Backbone.View({ el: $('.selector') }); // Using a jQuery object as the `el` View class property var MyView = Backbone.View.extend({     el:  $('.selector') }); 

What is a cached jQuery object?

It's a jQuery object assigned to a variable for reuse purpose. It avoids the costly operation of finding the element through the DOM with something like $(selector) each time.

Here's an example:

render: function() {     this.$el.html(this.template(/* ...snip... */));     // this is caching a jQuery object     this.$myCachedObject = this.$('.selector'); },  onExampleEvent: function(e) {     // Then it avoids $('.selector') here and on any sub-sequent "example" events.     this.$myCachedObject.toggleClass('example'); } 

See an extensive answer I wrote to know more.

like image 37
Emile Bergeron Avatar answered Sep 18 '22 07:09

Emile Bergeron


lets say that you do this

var myel = this.el; // here what you have is the html element,                      //you will be able to access(read/modify) the html                      //properties of this element, 

with this

var my$el = this.$el; // you will have the element but                        //with all of the functions that jQuery provides like,                       //hide,show  etc, its the equivalent of $('#myel').show();                       //$('#myel').hide(); so this.$el keeps a reference to your                        //element so you don't need to traverse the DOM to find the                       // element every time you use it. with the performance benefits                        //that this implies. 

one is the html element and the other is the jQuery object of the element.

like image 57
Rayweb_on Avatar answered Sep 18 '22 07:09

Rayweb_on