Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the id of a View in Backbone.js?

In Backbone.js when I create a view I can initialise the 'class' attribute of it's element as shown below:

var newView = Backbone.View.extend({
    className: 'foo'
});

Is there a similar way to set the 'id' attribute in a similar way?

like image 314
RobotEyes Avatar asked Oct 26 '12 20:10

RobotEyes


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.

Who created backbone JS?

Backbone was created by Jeremy Ashkenas, who is also known for CoffeeScript and Underscore. js.

Does backbone need jQuery?

You can use the Backbone. Model without jQuery, but Backbone.


2 Answers

var newView = Backbone.View.extend({
    id: 'foo1',  // or
    tagName: 'foo2', // or ..
    className: 'foo3' // 
});

Hope that helps!

like image 134
Jim Avatar answered Sep 23 '22 10:09

Jim


There is an existing id property you can use. Here's a relevant portion from the docs:

elview.el All views have a DOM element at all times (the el property), whether they've already been inserted into the page or not. In this fashion, views can be rendered at any time, and inserted into the DOM all at once, in order to get high-performance UI rendering with as few reflows and repaints as possible. this.el is created from the view's tagName, className, id and attributes properties, if specified. If not, el is an empty div.

Also, you can bind to an existing element that is already in the HTML if that works better for you.

like image 21
John Munsch Avatar answered Sep 23 '22 10:09

John Munsch