Using knockout’s mapping plugin, how would I go about displaying the information from and array of JSON object’s in my template.?
//Example JSON data.
JSON = [{'title':'title of first object',
'content':'content of first json object'},
{'title':'title of second object', 'content': 'content of second json object'}]
var viewModel = ko.mapping.fromJS(data);
var data = $.getJSON('/post/.json', function(data) {
ko.mapping.fromJS(data, viewModel);
});
I’m able to access the objects via viewModel() and viewModel()[0].title() using Console in Chrome but not sure how to bind this date to my template.
After you receive your data, you would need to call
ko.applyBindings(viewModel).In your HTML, you can bind against your data like:
Here is a sample: http://jsfiddle.net/rniemeyer/YkT6p/