I am building a permissions UI, I have a list of permissions with a select list next to each permission. The permissions are represented by an observable array of objects which are bound to a select list:
<div data-bind="foreach: permissions">
<div class="permission_row">
<span data-bind="text: name"></span>
<select data-bind="value: level, event:{ change: $parent.permissionChanged}">
<option value="0"></option>
<option value="1">R</option>
<option value="2">RW</option>
</select>
</div>
</div>
Now the problem is this: the change event gets raised when the UI is just populating for the first time. I call my ajax function, get the permissions list and then the event get raised for each of the permission items. This is really not the behavior I want. I want it to be raised only when a user really picks out a new value for the permission in the select list, how can I do that?
Quick and dirty, utilizing a simple flag:
If this doesn't work, try wrapping the last line in a setTimeout() - events are async, so maybe the last one is still pending when applyBindings() already returned.
I use this custom binding (based on this fiddle by RP Niemeyer, see his answer to this question), which makes sure the numeric value is properly converted from string to number (as suggested by the solution of Michael Best):
Javascript:
Example HTML:
I had a similar problem and I just modified the event handler to check the type of the variable. The type is only set after the user selects a value, not when the page is first loaded.
This seems to work for me.
This is just a guess, but I think it's happening because
level
is a number. In that case, thevalue
binding will trigger achange
event to updatelevel
with the string value. You can fix this, therefore, by making surelevel
is a string to start with.Additionally, the more "Knockout" way of doing this is to not use event handlers, but to use observables and subscriptions. Make
level
an observable and then add a subscription to it, which will get run wheneverlevel
changes.Here is a solution that may help with this strange behaviour. I couldn't find a better solution than place a button to manually trigger the change event.
EDIT: Maybe a custom binding like this could help:
And in your select data-bind attribute add:
Actually you want to find whether the event is triggered by user or program , and its obvious that event will trigger while initialization.
The knockout approach of adding
subscription
won't help in all cases, why because in most of the model will be implemented like this(actual KO initilization)
(logical init like load JSON , get data etc)
The actual step that we want to capture is changes in 3, but in second step
subscription
will get call , So a better way is to add to event change likeand detected the event in
permissionChanged
function