Crumblenautjs Crumblenautjs - 3 years ago 136
Javascript Question

KnockoutJS : Create binding handler to change value to enum

I have a knockout custom binding that takes a value and returns an enum. This is for user readability. The problem is that when passing in the parameter for this binding, it just comes back blank. I've tried passing in the parameter as an observable, ex: PurchaseOrderStatus[Status()].

The view:

<td style="vertical-align: top">
<label>Status</label><br />
<span data-bind="text: PurchaseOrderStatus[Status]"></span>

The javascript :

PurchaseOrderStatus = function() { };
PurchaseOrderStatus.prototype = {
Closed: 67,
Deleted: 68,
Finalized: 70,
Open: 79}
PurchaseOrderStatus.registerEnum('PurchaseOrderStatus', false);

The way it should work is that if the value is 70, then the user sees Finalized.

Answer Source

how about something like this.

function model() {
  var self = this;
  this.PurchaseOrderStatus = ko.observable({
    '67': 'Closed',
    '68': 'Deleted',
    '70': 'Finalized',
    '79': 'Open'
  this.status = ko.observable('68');

var mymodel = new model();

$(document).ready(function() {
<script src=""></script>
<script src=""></script>

<span data-bind="text: PurchaseOrderStatus()[status()]"></span>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download