1

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

2
  • 2
    you're right... that was kinda rude. I marked some good answers as 'answered'... Commented May 15, 2012 at 4:15
  • I don't understand why you would want to create a script block for each element in your foreach, can you give a more complete example? Commented May 15, 2012 at 5:59

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.