knockout / knockout

Knockout makes it easier to create rich, responsive UIs with JavaScript
http://knockoutjs.com/
Other
10.45k stars 1.52k forks source link

Make optionsCaption handle observables #205

Closed barclayadam closed 12 years ago

barclayadam commented 12 years ago

Steve,

We have the need to display an option caption that changes based on the state of the system (e.g. 'Loading' -> 'Please Select'). To achieve this I've made a change to the options binding handler to unwrap the optionsCaption observable:

option.innerHTML = allBindings['optionsCaption'];

becomes

option.innerHTML = ko.utils.unwrapObservable(allBindings['optionsCaption']);

on line 213 of src/binding/defaultBindings.js

This should be a non-breaking change that can hopefully be slipped into the 1.3.0 release if that ship has not already sailed?

If required I can create a pull request when I can find the time.

Thanks, Adam

rniemeyer commented 12 years ago

Seems like a harmless, worthwhile change.

Just wanted to mention that you can do this right now by doing: data-bind="options: choices, optionsCaption: myCaption()"

This works because bindings are implemented inside of a dependentObservable, so the evaluation of myCaption will create a dependency. Additionally, the optionsCaption binding does not need the actual observable, as it does not need to operate on the observable itself (like write to it).

Here is a sample: http://jsfiddle.net/rniemeyer/hQVYJ/

SteveSanderson commented 12 years ago

Adam, thanks for this. We can look at putting this into 1.3.1. In order for 1.3.0 to ever reach its final release, the policy now is deferring changes that aren't strictly necessary :) Hope that's OK.

barclayadam commented 12 years ago

Steve, makes absolute sense. Being that this is a easily worked-around 'issue' no problem delaying, no point in endlessly delaying the next release :)

SteveSanderson commented 12 years ago

Agreed we include this in 2.2

SteveSanderson commented 12 years ago

Pull request here: https://github.com/SteveSanderson/knockout/pull/543

mbest commented 12 years ago

Merged.