I think I have a pretty generic question, but it is for a specific application. I would simply like to know how it might be possible to set a custom attribute on a dropdown that equals the dropdown's initial value. That value will be bound from a KnockOut view model value, but I don't want to have it be bound permanently, so that if it changes, that attribute's value does not change:
<select data-bind="options: availableSlots, attr: { preset: SlotPosition }, event: {'change', myChangeRoutine} " ></select>
In the example above, preset
is my attribute, and I want to set it to SlotPosition
, which is in my viewmodel and is what the dropdown will initially automatically be set to. availableSlots
is a MVC server-side-created SelectListItem that has slots 1 through x number of items that I return into my viewmodel as a ko.observableArray(). (It is in the server-side code that I set Selected
to true where it needs to be set, for the given value, and build the options.)
The above example would bind my attribute preset
to my model's value SlotPosition
, but I just want it to remember that value until it is changed, instead of permanently binding to it. The issue I have is that in myChangeRoutine
, the value for preset
has already become what the dropdown was changed to. I want this to work so that when I change the dropdown's value and myChangeRoutine
runs, I can do something with the old value.
Just make your SlotPosition
as simple plain value, not ko.observable
, this will be one time binding.
Or use attr { preset : SlotPosition.peek() }
, see documentation.