knockout.js - Knockout get foreach to work -
i´m having object containing 3 notes
{ notes:[ { id:{type:"integer",value:"215356161"}, body:"", author_id:"980766", subject_id:"201674760", created_at:{date:"2014-03-24 16:50:14",timezone_type:2,timezone:"z"}, attachments:{ type:"array", attachments:[{ id:{type:"integer",value:"77791298"}, url:"https://fdgsgds.highrisehq.com/files/777915446298", name:"nav-rubik-03.mp3", size:"13954" ]} //typo } }, { id:{type:"integer",value:"215356129"}, body:"test",author_id:"980766", subject_id:"201674760", created_at:{date:"2014-03-24 16:50:08",timezone_type:2,timezone:"z"} }, { ... }] } i want iterate on it
the second foreach doesnt work
<div data-bind="foreach: appmodel.issue().highrisenotes()"> <p data-bind="text:$data.created_at.date"></p> <p data-bind="text:$data.body"></p> <!-- update, solution check if there attachments @ --> <!-- ko if:$data.attachments --> <div data-bind="foreach: $data.attachments.attachments)"> <p data-bind="text:$data"> </p> </div> <!-- /ko --> </div> resulting in
uncaught typeerror: unable process binding "text: function (){return $data.attachments.attachments }" message: cannot read property 'attachments' of undefined how iterate on "attachments" in "attachments"
one of elements in array missing attachment property, resulting in binding failure experienced.
a few solutions.
1 . wrap inner foreach in if binding
<!-- ko if:$data.attachments --> <div data-bind="foreach..." /> <!-- /ko --> 2 . ensure have attachments property in each notes element. may require change json provider or serialiser, or making sure attachments property server side either empty array or list, depending on technology stack.
{ notes:[ { ... attachments: { type:"array", attachments:[] } } } 3 . change binding default empty object.
<div data-bind="foreach: ($data.attachments || {}).attachments)">
Comments
Post a Comment