Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 'el' do in view.render().el?

I am trying to learn backbone. I understand that el is the element that is being acted on. If it isnt specified it is an empty div. I am creating a template inside my view and returning the view object. I am then rendering the view, but I dont understand why I chain el after the render function. Could someone please explain this line of code to me:

var view = new PersonView();
this.$('#family_list').children().append(view.render().el);

What is the el for? Thanks.

like image 328
jhamm Avatar asked Dec 01 '12 13:12

jhamm


People also ask

What is El in 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.

What is Backbone used for?

Backbone is known for being lightweight, as its only hard dependency is on one JavaScript library, Underscore. js, plus jQuery for use of the full library. It is designed for developing single-page web applications, and for keeping various parts of web applications (e.g. multiple clients and the server) synchronized.


1 Answers

jQuery's .append() method expects a string of HTML or a DOM element to be appended to its calling node.

The .el property of the view is its bound DOM element. After calling view.render(), its .el DOM element property is passed into the jQuery .append() method, so jQuery .append() gets the updated (newly rendered) DOM node.

This is made possible because the .render() call must be returning this as suggested in the documentation. Its return value is therefore the view object itself1, and from that the .el can be referenced immediately.


1 Wikipedia: Fluent interfaces

like image 90
Michael Berkowski Avatar answered Nov 06 '22 05:11

Michael Berkowski