Reputation: 19862
I am learning Knockout JS and following their online interactive tutorial.
I have a button to add a new reservation.
<button data-bind="click: addSeat($('#passenger-name').val(), $('#selected-meal').val()), enable: seats().length < 5">Reserve another seat</button>
However the issue is that the addSeat
function is called initially when Knockout is evaluating the binding expressions (or so I assume). It adds an empty row when the page loads.
How do I stop that from happening ?
Upvotes: 0
Views: 38
Reputation: 4073
The function addSeat is invoked because you invoke it in binding. Click binding expect to receive reference to function, not code that should be invoked. So in order to make call that you want, you have to wrap it in function.
<button data-bind="click: function () { addSeat($('#passenger-name').val(), $('#selected-meal').val()); }, enable: seats().length < 5">Reserve another seat</button>
Here is updated example: http://jsfiddle.net/9kk0haga/
But in your case even better solution would be to replace jQuery code to use knockout binding: http://jsfiddle.net/9kk0haga/1/
Upvotes: 2