Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone.js and el attribute

(function ($) {
window.AppView = Backbone.View.extend({
  el: $("body"),
  events: {
    "click #add-friend":  "showPrompt",
  },
  showPrompt: function () {
    var friend_name = prompt("Who is your friend?");
  }
});
var appview = new AppView;
})(jQuery);
  1. Can anyone explain me what is el here. Is it element?
  2. Does the el argument accept object, if so can i pass my custom view object where my button or elements need to be added...
like image 927
John Cooper Avatar asked May 25 '12 06:05

John Cooper


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.

Is Backbone JS still used?

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.

What are the main components of backbone JS?

Unlike most of the MVC frameworks, Backbone. js consists of six main components: Models, Views, Collections, Events, Routers and Sync.

Is Backbone JS frontend or backend?

Backend Synchronization BackboneJS is use with the front-end and back-end systems, allows the synchronization with the backend to provide support to RESTful APIs.


2 Answers

  1. Yes it's a DOM element.
  2. No you cannot pass a custom object. You either specify an existing element, or creating one from the tagName, className, id and attributes properties of the view. If you don't specify an element, it defaults to a div

It's all in the official documentation actually...

like image 64
Alladinian Avatar answered Sep 21 '22 22:09

Alladinian


Alladnian answered it but I would add that when using el you can make use of $el which is a cached jQuery object of your view element.

So you can always simply pass only the tag you wish to use (for consistency, brevity and flexibility) and then reference it as $el to make use of it as a jQuery object.

this.$el.addClass("active");
like image 37
rg88 Avatar answered Sep 19 '22 22:09

rg88