Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change event on select with knockout binding, how can I know if it is a real change?

Tags:

knockout.js

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?

like image 935
guy schaller Avatar asked Jun 18 '12 06:06

guy schaller


People also ask

What is binding in Knockout?

This binding is used to bind the child elements of an object in the specified object's context. This binding can also be nested with other type of bindings such as if and foreach. Syntax with: <binding-object> Parameters. Pass the object which you want to use as context for binding child elements as the parameter.

How do you activate Knockout?

To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.

What is a Knockout observable?

Knockout. js defines an important role when we want to detect and respond to changes on one object, we uses the observable. An observable is useful in various scenarios where we are displaying or editing multiple values and require repeated sections of the UI to appear and disappear as items are inserted and deleted.


1 Answers

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

  1. init the model with undefined data , just structure (actual KO initilization)
  2. update the model with initial data (logical init like load JSON , get data etc)
  3. User interaction and updates

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 like

 <select data-bind="value: level, event:{ change: $parent.permissionChanged}"> 

and detected the event in permissionChanged function

this.permissionChanged = function (obj, event) {    if (event.originalEvent) { //user changed    } else { // program changed    }  } 
like image 118
Sarath Avatar answered Sep 25 '22 04:09

Sarath