I have a view that creates and populates a Selectlist. I want to bind an event on the Change event (When the user selects a different Option.
It simply outputs something close to this:
<select>
<option value="id">ID Name</option
</select>
The View:
App.Views.SessionList = Backbone.View.extend({
tagName: 'select',
id: 'sessionList',
events: {
'change': 'selectionChanged'
},
initialize: function () {
// Load Collection
_.bindAll(this, 'selectionChanged');
this.template = _.template($('#optionsTemplate').html());
this.Sessiontemplate = _.template($('#session_template').html());
this.SelectedSessiontemplate = _.template($('#selected_session_template').html());
// Create Collection
this.SessionList = new App.Collections.SessionList();
// Set Bindings
this.SessionList.bind('add', this.addSession, this);
this.SessionList.bind('reset', this.addSessions, this);
this.render();
this.SessionList.fetch();
},
render: function () {
return this;
},
addSession: function (item, num) {
if (num === 0) {
$(this.el).append(this.SelectedSessiontemplate(item.toJSON()));
console.log("Selected");
}
else {
$(this.el).append(this.Sessiontemplate(item.toJSON()));
}
},
addSessions: function () {
var self = this;
// Add all Rows
for (var i = 0; i < self.SessionList.models.length; i++) {
this.addSession(self.SessionList.models[i], i);
}
},
selectionChanged: function (e) {
var field = $(e.currentTarget);
var value = $("option:selected", field).val();
}
});
The Session Template is just simply:
<option value="{{Id}}">{{Name}}</option>
The event never gets triggered, and It seems that it isn't getting binded correctly. I want to trigger it on the change of the select list.
I originally thought I may be having an issue similar to: backbone.js events and el, however it doesn't work in this case.