Paul
Paul

Reputation: 165

Knockout Databind Event Fires twice

Im Rendering a Option Select including an Change Event. After Rendering the Event fires twice... i cant figure why...

  <select id="@Model.ID)" 
          data-bind = "@string.Format("
          options: groupItems({0}), 
          optionsText : 'description',  
          optionsValue : 'id', 
          optionsAfterRender: afterDropDownRender, 
          value : selectedItem({0}),  
          event: {{ change: selectionChanged }}", Model.GroupID)"></select>

The Event "selectionChanged" is fireing twice after changeing a selection.

Upvotes: 1

Views: 1161

Answers (3)

Paul
Paul

Reputation: 165

Ok, found the cause, although not quite clear why..

when Drilling down and left out

value: selectedItem(2),

the Event doesnt fire twice..

no diving into "selectedItem" :)

Upvotes: 0

Sorskoot
Sorskoot

Reputation: 10310

Try to find out what is rendered on the client. It should look something like:

<select id="IdFromServer" data-bind = "options: groupItems(2),                     
          optionsText: 'description',   
          optionsValue : 'id',                
          value : selectedItem(2),  
          event: { change: selectionChanged }"></select>

Fiddle

Upvotes: 1

Wayne Ellery
Wayne Ellery

Reputation: 7958

I think your syntax is not quite right. You just need to bind options to groupItems and not groupItems({0}). Same goes for selected Item. The resulting html should be:

<select data-bind ="options: groupItems, 
          optionsText : 'description',  
          optionsValue : 'id', 
          optionsAfterRender: afterDropDownRender, 
          value : selectedItem,  
          event: { change: selectionChanged }"></select>

http://jsfiddle.net/MXMv5/

Upvotes: 0

Related Questions