Backbone.js and the 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); 
  • Can someone explain to me that here is el . Is this an item?
  • Does the el argument provide an object, if I can pass my custom view to an object where I need to add my button or elements ...
+7
source share
2 answers
  • Yes, this is a DOM element.
  • No, you cannot pass a custom object. You either specify an existing element or create it from the tagName , className , id and attributes properties of the view. If you do not specify an element, the default will be a div

All this in the official documentation is actually ...

+5
source

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

That way, you can always just pass in the tag you want to use (for consistency, brevity and flexibility) and then reference it as $el to use it as a jQuery object.

 this.$el.addClass("active"); 
+2
source

All Articles