简体   繁体   中英

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?

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

Hope that helps!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM