0

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

2
  • 2
    just make a change like this ko.mapping.fromJS(initialTables)() fiddle here jsfiddle.net/supercool/udxr4/750 Commented Feb 5, 2015 at 5:57
  • @supercool, please don't be shy to make an answer instead of comments if it is really an answer. Firstly, it would give a chance to accept an answer for the TS, and secondly it would help the others to skip questions that have accepted answer already. Commented Feb 5, 2015 at 9:45

1 Answer 1

1

well arnab you are missing braces() which should do your work .

Please do the modification in this line

ko.observableArray(ko.mapping.fromJS(initialTables))

to something like

ko.observableArray(ko.mapping.fromJS(initialTables)()) 

To a observable array you need to pass Array . If you give () at the end you get a Array (unwrapping content) .

Working fiddle here

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.