Re: <button> click binding invoked at initialisation?

15 views
Skip to first unread message
Message has been deleted

noirabys

unread,
Jan 13, 2017, 1:51:12 AM1/13/17
to KnockoutJS
hi,

use an anonymous function if you want to call the function with an other parameter than $data
<button data-bind="click: function(){ addAgent('John')}">Add</button>

this would call addAgent with $data as paramater:
<button data-bind="click: addAgent">Add</button>

best regards,
 noirabys





Am Freitag, 13. Januar 2017 07:42:24 UTC+1 schrieb Henry Milne:
<button data-bind="click: addAgent('John')">Add</button>


Javascript:
// Overall viewmodel for this screen, along with initial state
function ReservationsViewModel() {
   
var self = this;

 
****some stuff deleted here****

   
self.addAgent = function(daname) {
       
self.seats.push(new SeatReservation(daname));
   
}
}


This works, however on opening the page the addAgent function is called and John is passed to it.  From the syntax this should only occur when the button is clicked - not just on loading? 

Any help would be great. 

(really love knockout.js btw!)

Many thanks.

Henry Milne

unread,
Jan 13, 2017, 2:17:46 AM1/13/17
to KnockoutJS
Thankyou so much Noirabys. I really appreciate it. 
Reply all
Reply to author
Forward
0 new messages