I would like to add a 'delete comment' button as a form that triggers an ajax request next to each comment that is generated using knockout's foreach binding. comments
is an observable array, each comment is an object with username
, text
, timestamp
and commentID
members. Loading the following without the <form>
element works fine:
<ul data-bind="foreach: comments">
<li>
<span data-bind="text: username"></span>
<ul>
<li data-bind="text: text"></li>
<li data-bind="text: timestamp"></li>
<form data-bind="if: sameUser" method="post" action="deleteComment.php">
<input data-bind="attr: {id: commentID}, click: deleteComment" type="submit" value="Delete comment">
</form>
</ul>
</li>
</ul>
However, including a <form>
element breaks the foreach loop, only one comment is loaded.
I want to use the if: sameUser
data-bind so that the delete button is only visible to the user that posted the comment, and the
attr: {id: commentID}
data-bind to send the right comment ID to delete to the server when the button is clicked, but right now my main concern is loading the form/button in the first place.
What is the correct way to go about this?
if: $parent.sameUser()
– IrkenInvader