Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BackboneJs: In a view whats the difference between el: and tagName:

I'm trying to wrap my head around this concept.

Can you dumb this down for me and maybe provide a simple example of the difference between the el: attribute and the tagName: attribute?

In some examples, different views use el: sometimes and others use tagName:.

I'm specifically messing around with my own implementation of this example

like image 691
Mike Fielden Avatar asked Sep 15 '11 14:09

Mike Fielden


People also ask

What is El property of backbone JS view?

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 jQuery El?

el is just an identifier and it refers to an element, a DOM element, which is a convention in that library.

What is backbone JS used for?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.


1 Answers

The difference is this:

el should be used to preserve a reference to the actual DOM node representing the view as a whole.

This means you can easily perform actions on it with jQuery or w/e. $(this.el).hide() or $(this.el).html('I am a Jquery Object now');

TagName is only a string that is used to determine the type of DOM node el will be. The default is div, but if you wanted, you could make it any HTML element you please.

Consider:

var view = Backbone.View.extend({
    tagName: 'p',
    initialize: function () {
        _.bindAll(this, 'render');
    },
    render: function() {
        $(this.el).html('I am a jQuery-ized paragraph');
        return this;
    }
});


$(document.body).append(new view().render().el);

The problem you might be running into is that sometimes you set el on the instantiation of a view, in which case the tagName is irrelevant:

var myView = new view({ el: $("someExistingEl") });
like image 78
Skylar Anderson Avatar answered Sep 28 '22 14:09

Skylar Anderson