I have this dropdown which have options if vehicle is new or used.
<select name="VehicleType" id="vehicleTypeDropdown" data-bind="value: VehicleType">
<option value="New" selected="selected">Nuevo</option>
<option value="Used">Usado</option>
</select> `
And this input:
<input type="text" name="Mileage" data-bind="disable: VehicleType() === 'New',value: Mileage" class="input-large"> `
If the value in the dropdown selected is New the input must be disabled, and if used the input should be enabled, but if I enter a value the observable will grab this value and if I change the dropdown value to new the observable must become zero.
If you're using the ko mapping plugin you can intercept the creation of a new object and set up the subscription there. If you have a whole list of items and you want to perform an action when something changes.
This is a view model for a shopping cart line item (properties such as
qty
,sku
,description
,price
).When you update (or create) your model using the mapping plugin you specify that for a property called 'items' each element in the array should be created with the 'create' function you specify.
A manual subscription in your view model is a good way to handle something like this. The subscription might look like:
Here is a sample using it: http://jsfiddle.net/rniemeyer/h4cKC/
The HTML:
The JS: