Complete knockout.js newbie here.
I’m having some trouble understanding how knockout.js works. Any help is appreciated.
I have a JSON model that I get back asp.net MVC that looks like somethings like this –
var model = [{
"Personal": {"Name" : "Something"},
"Address": [
{"City" : "Some City"}
{"City" : "Some Other City"}
{"City" : "3rd City"}
"Relationships": [
{"Affiliation" : "Aff1"},
{"Affiliation" : "Aff2"},
]
]
var viewModel = ko.mapping.fromJS(model);
ko.applyBindings(viewModel);
Then, in my view I’m trying to do this –
<div data-bind="template: {foreach: Address}">
<span data-bind="value: City"></span>
</div>
I get an ‘Unable to parse bindings, Address is not defined’ error.
What am I doing wrong?
What do you mean, it “looks something like this”?
Your javascript model object as written is not a valid one (missing commas, mismatched brackets..).
It looks like
modelis an array (not an object), and it doesn’t make much sense to pass an array toko.mapping.fromJS.It works fine with valid data (and changing the span’s binding to
textinstead ofvalue), see the fiddle: http://jsfiddle.net/KXhem/16/