As BaskingShark mentioned, the attributes hash can be used to specify attributes to the option elements.
Item View can be used for the option element.
var OptionView = Marionette.ItemView.extend({
tagName: 'option',
attributes: function () {
return {
'value': this.model.get('id')
}
},
template: _.template('<%= name %>')
});
A collection view that uses the item view can be used for the select tag.
var SelectOptionsView = Marionette.CollectionView.extend({
childView: OptionView
});
The collection view can be rendered inside a region of a layout view, where the events on the select tag can also be listened to.
Fiddle for an example: http://jsfiddle.net/95g1ojwk/3/
var pills = new Backbone.Collection([{
id: '',
name: 'Choose'
}, {
id: 'illusion',
name: 'Blue'
}, {
id: 'reality',
name: 'Red'
}]);
var OptionView = Marionette.ItemView.extend({
tagName: 'option',
attributes: function () {
return {
'value': this.model.get('id')
}
},
template: _.template('<%= name %>')
});
var SelectOptionsView = Marionette.CollectionView.extend({
childView: OptionView
});
var MyLayoutView = Mn.LayoutView.extend({
ui: {
destiny: "#destiny",
choice: '#choice'
},
regions: {
"options": "#options-region",
},
events: {
'change @ui.choice': 'chosenDestiny'
},
onRender: function () {
this.showChildView('options', new SelectOptionsView({
collection: pills,
el: this.ui.choice
}));
},
chosenDestiny: function (event) {
if($(event.target).val()==='reality'){
this.ui.destiny.text('Nothing but Truth!');
} else if($(event.target).val()==='illusion'){
this.ui.destiny.text('The Story Ends!');
} else {
this.ui.destiny.text('This is your last chance!');
}
}
});
new MyLayoutView({
el: '#app',
template: '#layout-template'
}).render();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<script src="http://marionettejs.com/downloads/backbone.marionette.js"></script>
<script type="text/template" id="layout-template">
<div id="options-region">
<select id="choice"></select>
</div >
<label id = "destiny" > </label>
</script>
<script type="text/template" id="image-template">
<div class = "sizer" > <img src = "http://placehold.it/<%= width %>x<%= height %>" /> </div>
</script>
<div id="app"></div>