How do you handle 'Paged" JSON data in KnockoutJS?

56 views
Skip to first unread message

Chris F.

unread,
Mar 28, 2018, 8:44:26 AM3/28/18
to KnockoutJS
Given a JSON response that looks like the following:

{
    "_items": [
        {
            "_updated": "Thu, 01 Jan 1970 00:00:00 GMT",
            "firstname": "George",
            "lastname": "Washington",
            "_etag": "974d659cab03554c4fad30018e4f37423f72cbfa",
            "_links": {
                "self": {
                    "href": "people/1",
                    "title": "People"
                }
            },
            "_created": "Thu, 01 Jan 1970 00:00:00 GMT",
            "fullname": "George Washington",
            "id": 1
        },
        {
            "_updated": "Thu, 01 Jan 1970 00:00:00 GMT",
            "firstname": "John",
            "lastname": "Adams",
            "_etag": "0b923e8eee13ce5264072f7768b19283dc3c362f",
            "_links": {
                "self": {
                    "href": "people/2",
                    "title": "People"
                }
            },
            "_created": "Thu, 01 Jan 1970 00:00:00 GMT",
            "fullname": "John Adams",
            "id": 2
        },
        {
            "_updated": "Thu, 01 Jan 1970 00:00:00 GMT",
            "firstname": "Thomas",
            "lastname": "Jefferson",
            "_etag": "b70715c7332dd2bdb6a24a1539b531ffe0166bd1",
            "_links": {
                "self": {
                    "href": "people/3",
                    "title": "People"
                }
            },
            "_created": "Thu, 01 Jan 1970 00:00:00 GMT",
            "fullname": "Thomas Jefferson",
            "id": 3
        }
    ],
    "_links": {
        "self": {
            "href": "people",
            "title": "people"
        },
        "parent": {
            "href": "/",
            "title": "home"
        }
    },
    "_meta": {
        "max_results": 25,
        "total": 3,
        "page": 1
    }
}

 how do I access the items in _items and the _meta data so I can page through my data?  Could someone point me in the right direction please?  I can only find examples at KnockoutJS.com that deal with JSON datasets that are 'flat'.

My <body> contains the following debug code:

<div data-bind="text: ko.toJSON(peopleViewModel)"></div>

which displays:

{"peopleURI":"http://localhost:8000/people","people":[]}


I don't understand how to map _items to the peopleViewModel and _meta to my paging controls.

Sorry for the Noob Question.  New to JavaScript and KnockoutJS. Thanks.
Reply all
Reply to author
Forward
0 new messages