1
votes

In the following Fiddle I would like to be able to set the selectedFruit when clicking on one of the fruits in the list however this does not appear to be working.

After clicking on a fruit, if I inspect the value of viewModel.selectedFruit in a Chrome immediate window it appears to set the value to the viewModel object rather than the bound data item. What am I doing wrong? It appears that there is a loss of context somewhere?

JSFiddle link

Thanks

Conclusion

Whilst this helped track down the problem I was experiencing in my main app, the underlying problem was that in haste I had forgotten to use the KO template foreach binding on my <UL> and was using the templ foreach instead.

1

1 Answers

1
votes

Your viewModel variable does not have global scope, as the fiddle's options are set to onLoad for how the JavaScript is loaded. If you switch it to no wrap (body), then you will be in good shape.

The only other change is that you can just use $data directly, rather than $data().

http://jsfiddle.net/rniemeyer/LXssJ/10/