0

so I have my observable array

answer: ko.observableArray("")

and I'm trying to use data-bind to access just answer()[0]

<div id="iAnswer" style="display: inline-block; text-align: right; width: 80%; border: 0px;"
                     data-bind="dxTextBox: { value: answer()[$data.index], text:$data.answer}"></div>

$data.index is equal to 0 when its getting passed through.

Cody Jones
  • 424
  • 1
  • 5
  • 16
  • Can you please show the full code including the data that is in the answer array. This isn't enough to see an issue – Wayne Ellery Oct 28 '14 at 23:13
  • There isn't any data in the array, sometimes there is data, but the majority of the time it will be empty. The issue is how to data-bind just 1 index to the textbox. Its being loaded dynamically using a multiview in devextreme. Heres the done on paste bin http://pastebin.com/1aWJdrWb http://pastebin.com/YcDjFVh4 – Cody Jones Oct 28 '14 at 23:27
  • How can you access an index of the array if there is no elements in the array? – Wayne Ellery Oct 28 '14 at 23:28
  • Are you trying to add one answer that it empty string? – Wayne Ellery Oct 28 '14 at 23:31
  • array[0] <-- even though that is empty it should still return with nothing. I dont mind receiving an empty array, in fact that is anticipated. The problem is I'm recieving an error. – Cody Jones Oct 28 '14 at 23:32
  • Where is $data.index coming from? Are you in a loop – Wayne Ellery Oct 28 '14 at 23:39
  • dxTemplate creates $data. dxMultiview creates views based on the data source its given, and it creates these views using dxTemplate to model it. – Cody Jones Oct 28 '14 at 23:40

1 Answers1

0

Does this code solve your task?

<!-- ko if: answer().length === 0 -->
    <div> There are no answers. </div>
<-- /ko -->
<!-- ko ifnot: answer().length === 0 -->
<div id="iAnswer" style="display: inline-block; text-align: right; width: 80%; border: 0px;"
          data-bind="dxTextBox: { text:$data.answer }"></div>                
<-- /ko -->
TSV
  • 7,538
  • 1
  • 29
  • 37
  • Actually, I wasn't properly initializing the KO observables and my values weren't changing. I figured it out, thanks though. – Cody Jones Oct 29 '14 at 21:02