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?
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.
Backbone was created by Jeremy Ashkenas, who is also known for CoffeeScript and Underscore. js.
You can use the Backbone. Model without jQuery, but Backbone.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With