A) still update the viewmodel but have it not fire the onchange event for the select, but without breaking the dependent observables?
From the Knockout documentation: "If you try to set a model value that can't be represented in an already-populated dropdown, reject that change, because you're not allowed to have a model value that disagrees with a visible UI selection." This comment appears right before it fires a change
event in the value
handler.
So there are two possibilities for why you'll get a change
event from a programmatic viewmodel update:
You are setting a value that doesn't exist in the drop-down list. If this is the case, you can add code to check if the value is valid before setting it.
You are setting the value as a numeric type, although the values of the items in the drop-down list are strings. Although Knockout will select the matching item, it fires the
change
event to signal that the observable needs to be updated to the matching string value. If this is the case, you can make sure you convert the value to a string when setting the observable.
B) in the event delegation above to sniff out and differentiate between when a user causes an onchange event and when the viewmodel update causes the onchange event?
I haven't been able to determine which browsers support this, but the event may have an isTrusted
property that you can check. Reference: http://www.w3.org/TR/DOM-Level-3-Events/#h3_trusted-events