Reputation: 191
Update the key issue here is an impedance mismatch between the ruby objects and associated methods we have now (handled in erb templates) and pure viewModels in JSON which we can feed to KnockoutJs etc. I'll create KO-friendly viewModels w/all of the needed info for this area of the product and change other areas to use KO opportunistically...
I'm new to KnockoutJs. I have my viewModel set up and can display values using the 'comment form'foreach like this
<!-- ko foreach: filteredProducts -->
<span>id:</span>
<span data-bind="text: id"></span>
(...)
<br/>
<!-- /ko -->
But what I need to do is reference one of the array entry values inside my foreach like this
<script> current_id = $data.id </script>
So that I can use the Id via ERB to call a Ruby function. But $data is not visible to script here.
The viewModel is visible but similar problem: visibility of $index (another KnockoutJs context variable for foreach) in script.
Ideas welcome - Michael
Upvotes: 1
Views: 511
Reputation: 16688
The answer is that you can't and shouldn't. You can't reference $index
from within an embedded script. And you shouldn't embed a script in a Knockout foreach
block; you will likely not like the result.
Use bindings to interact between your view model (javascript) and your view (html). Use a custom binding if you need to.
Upvotes: 2