Book Image

KnockoutJS Web Development

Book Image

KnockoutJS Web Development

Overview of this book

Table of Contents (13 chapters)
KnockoutJS Web Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

JSON done Knockout style


JSON is the famous data packaging standard that seems to have taken over the Internet. At one time there was XML; RoR has its own packaging standard; and even Adobe had perhaps the best performing way to package and deliver data between the client and the server called Action Message Format (AMF). The overall winner seems to be JSON because it is simple and based on the most common development platform of all: JavaScript. If you would like more information about JSON you can visit http://json.org.

To serve our purpose, let's create a file called, json.html. The first thing we will do is convert a ViewModel to JSON. Most modern browsers have a function called JSON.stringify but that is not how we do it in Knockout. There are two methods included in Knockout:

  • ko.toJS

  • ko.toJSON

The first method, ko.toJS, will clone the Knockout data into a plain copy that contains no Knockout-related hints or information. The second method, ko.toJSON, will perform the ko.toJS action...