I'm trying to learn knockoutjs by studying existing sourcecodes and making small changes to them.. The source code that I'm trying to understand here is an example of knockout-sortable.js by RP Niemeyer.
The original example code is here.
The intent is to be able to get data from server in either json string or jtoken object and use it in code..
I make a small change to it and do a console.log to get json here
var initialTables = [
new Table("Table One", [
new Student(1, "Bobby", "male"),
new Student(2, "Ted", "male"),
new Student(3, "Jim", "male")
]),
new Table("Table Two", [
new Student(4, "Michelle", "female"),
new Student(5, "Erin", "female"),
new Student(6, "Chase", "male")
]),
new Table("Table Three", [
new Student(7, "Denise", "female"),
new Student(8, "Chip", "male"),
new Student(9, "Kylie", "female")
]),
new Table("Table Four", [
new Student(10, "Cheryl", "female"),
new Student(11, "Doug", "male"),
new Student(12, "Connor", "male")
]),
new Table("Table Five", [
new Student(13, "Cody", "male"),
new Student(14, "Farrah", "female"),
new Student(15, "Lyla", "female")
])
];
this.tables = ko.observableArray(initialTables);
The initialTables object gets converted to json using console.log(ko.toJSON(this.tables)) which can be seen in console.log and is shown below.
[{"students":[{"id":1,"name":"Bobby","gender":"male"},{"id":2,"name":"Ted","gender":"male"},{"id":3,"name":"Jim","gender":"male"}]},{"students":[{"id":4,"name":"Michelle","gender":"female"},{"id":5,"name":"Erin","gender":"female"},{"id":6,"name":"Chase","gender":"male"}]},{"students":[{"id":7,"name":"Denise","gender":"female"},{"id":8,"name":"Chip","gender":"male"},{"id":9,"name":"Kylie","gender":"female"}]},{"students":[{"id":10,"name":"Cheryl","gender":"female"},{"id":11,"name":"Doug","gender":"male"},{"id":12,"name":"Connor","gender":"male"}]},{"students":[{"id":13,"name":"Cody","gender":"male"},{"id":14,"name":"Farrah","gender":"female"},{"id":15,"name":"Lyla","gender":"female"}]}]
Now I change initialTables to the json object I recd earlier and call tables as shown below and here
this.tables = ko.observableArray(ko.mapping.fromJS(initialTables));
Now, even though I do not see any errors, I do not see the initialTables object reflected in the results pane..
What am I doing wrong??
All help is sincerely appreciated..
Thanks
ko.mapping.fromJS(initialTables)()fiddle here jsfiddle.net/supercool/udxr4/750