I am trying to update a table after editing a record using splicing but I am getting an error like below. The gender is a radio button. I even tried to remove that, the next field shows an error. What am I doing wrong here?
Uncaught ReferenceError: Unable to parse bindings. Bindings value: text: gender Message: gender is not defined
I saw a similar post Splicing new array of items onto existing Knockout observable array causes binding errors but it does not seem to work for me.
View Model
var vmSearchResultsModel = function ()
{
var self = this;
self.SearchResults = ko.observableArray([]); //Holds the results of the search
}
The data returned to the observable array
"SearchResults": [
{
"id": 3,
"name": "Adrian D'Costa",
"dob": "/Date(-37776600000)/",
"gender": "M",
"joindate": "/Date(-37776600000)/" //<-- this is another issue I need to fix
},
{
"id": 14,
"name": "Janet D'Curz",
"dob": "/Date(-37776600000)/",
"gender": "F",
"joindate": "/Date(-37776600000)/"
}
]
Splicing
var getjsondata = ko.toJSON(self.SearchResults, ['name', 'gender', 'dob', 'joindate']) // select only what is required to show
console.log(getjsondata);
var obj = JSON.parse(getjsondata); // convert JSON to JS string
alert(obj[0].gender); //M Male, F Female
self.SearchResults.splice(self.CurrentIndex, 1, obj); // updates the row that was edited <-- shows an error here
The radio button binding while editing
<td> Gender </td>
<td>
<input type="radio" id="rdMale" name="Gender" value='M' data-bind="checked: $root.gender" />
<input type="radio" id="rdFemale" name="Gender" value='F' data-bind="checked: $root.gender" />
</td>
EDIT 1 The below template shows when I search for some data based on DOB, JoinDate...
<script type="text/html" id="TmplSearchResults">
<tr style="border-bottom: 1px solid #CCC;">
<td valign="middle" data-bind="text: name"></td>
<td valign="middle" align="center" data-bind="text: gender"></td>
<!-- ko if: ($root.SearchByVal() ==="DOB") -->
<td valign="middle" data-bind="textualDate: dob"></td>
<!-- /ko -->
<!-- ko if: ($root.SearchByVal()==="Join Date") -->
<td valign="middle" data-bind="textualDate: joindate"></td>
<!-- /ko -->
<td valign="middle" > <img type="image" title="edit" src="images/edit1.png" data-bind="event:{ click: $root.EditEmpDetails.bind($data, $index())}" /></td>
</tr>
</script>
EDIT 2
I changed the code like this
self.SearchResults().splice(self.CurrentIndex, 1, obj);
Now the Uncaught ReferenceError: Unable to parse bindings. Bindings value: text: gender Message: gender is not defined error does not show but the row does not get updated with the changes. Where am I wrong?
EDIT 3
Jfiddle http://jsfiddle.net/7LYad/1/
UPDATE
This fiddle is the one without any errors but the splicing does not take place nor the observables get update. Where am I wrong. Should I have two view models, one for showing the grid and one for saving, and one for editing, saving and splicing?