Seeker
Seeker

Reputation: 1917

Knockout Observables in Obvservable Array Not Updating

Hi i am using knockout observables in ObservableArray the object that is pushed inside the observableArray is something like following and i am making its 'value' property as observable:

{
   'name': "aka[]",
   'value': (value && value != "" ? ko.observable(value) : ko.observable(""))
}

I am using the foreach inside my HTML to draw the input elements to the number of object found in observableArray (akaList below)

<p data-bind="foreach: $root.akaList()">
    <input title="Also Known As (AKA)" data-bind="attr:{ 'name' : $data.name, 'value': $data.value}" type="text"
                   required="required"/>
</p>

When i type in the input element something it doesn't update the object value inside observableArray. I found that foreach unwrap the observable array so thats why i can't see the updates in the array. So i tried doing it with $parent.akaList()[$index()].value and my HTML becomes like this:

<p data-bind="foreach: $root.akaList()">
    <input title="Also Known As (AKA)" data-bind="attr:{ 'name' : $data.name, 'value': $parent.akaList()[$index()].value}" type="text"
                   required="required"/>
</p>

but its still not working can you please tell me what is wrong ?I can't use mapping plugin due to some restrictions in my project. I am trying to find other way. am i writing something in wrong syntax? Here is fiddle below as well:

JS Fiddle

Upvotes: 2

Views: 50

Answers (1)

Dandy
Dandy

Reputation: 2177

You can use value binding as

<input title="Also Known As (AKA)" data-bind="value: $data.value, attr:{ 'name' : $data.name}" type="text" required="required" />

If you can use knockout 3.2.0, then use textInput binding.

<input title="Also Known As (AKA)" data-bind="textInput: $data.value, attr:{ 'name' : $data.name}" type="text" required="required" />

JsFiddle: https://jsfiddle.net/29Lvhrx4/2/

Upvotes: 1

Related Questions