Within my model I have an object array with observable elements. I have a subscribe on one of these elements (a dropdown list) in order to fill in the next dropdown (cascading dropdowns). I now need to get the description of the selected value in these dropdowns. How would I pull that value from the dropdown list? I am trying to avoid running a query against the server if possible.
In the past I have done self.deptName($('#AssignedDepartment option:selected').text());
but now this dropdown isn't unique. I could have many to choose from. I am using uniqueName: true
on each of the fields. How, within the subscribe, do I determine which dropdown changed?
Here is what my subscribe looks like:
// Build the hours types when the request type is selected
self.RequestType.subscribe(function (newValue) {
FindRequestType(newValue, function (record) {
// Do stuff
});
});
Here is the HTML for the dropdown:
<select class="required span12" data-bind="leaveTypeDropDown: RequestType, uniqueName: true"></select>
Here is the bindingHandler:
ko.bindingHandlers.leaveTypeDropDown = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// If we don't have the leave request dropdown data, pull it.
if (jsonRequestType === "") {
$.ajax({
'async': false,
'url': '/api/payroll/LeaveRequestTypes/',
'dataType': 'text json',
'type': 'GET',
'success': function (json) {
jsonRequestType = json;
jsonRequestTypeRecieved = true;
}
});
}
// Fill the drop down list.
LoadRequestType(element);
// register event handler where bound observable will be changed
ko.utils.registerEventHandler(element, 'change', function () {
var observable = valueAccessor();
observable(element.value);
});
// Select the value from the previous record if there is one.
if (selectedRequestType !== null) {
element.value = selectedRequestType;
ko.utils.triggerEvent(element, 'change');
selectedRequestType = null;
}
}
};
Update 1: Since there is confusion. If I select the value "VAC" with the text of "Vacation" in my dropdown list. How do I get the text "Vacation" into my model?
Update 2: Here is a jsFiddle with my (mostly) working code as listed above. http://jsfiddle.net/MikeWills/cykuqxto/6/ For some reason, on load my drop downs don't work, but add another day and you'll see my working dropdowns.