Async Data Binding

How to update bindings when using asynchronous JavaScript.

by Jim Marion

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>
<div id="div1">
  <h2>
    Visible Table (Table 1)
  </h2>
  <table id="table" summary="Employee Table 1" aria-label="Employee Table 1"
         data-bind="ojComponent: {component: 'ojTable',
                    data: datasource,
                    columnsDefault: {sortable: 'none'},
                    columns: [{headerText: 'Employee Id',
                    field: 'id'},
                    {headerText: 'Employee Name',
                    field: 'name'}]}">
  </table>
  
  <h2>
    Hidden Table (Table 2)
  </h2>
  <!-- ko if: haveEmployees --> 
    <table id="table" summary="Employee Table 2" aria-label="Employee Table"
           data-bind="ojComponent: {component: 'ojTable',
                      data: datasource,
                      columnsDefault: {sortable: 'none'},
                      columns: [{headerText: 'Employee Id',
                      field: 'id'},
                      {headerText: 'Employee Name',
                      field: 'name'}]}">
    </table>
  <!-- /ko -->  
</div>

CSS

@import url('//cdn.rawgit.com/oracle/oraclejet/49fcbbde7c8e178a3a21625d44485536e3ad1aaf/dist/css/alta/oj-alta.css');

JavaScript

requirejs.config({
  // Path mappings for the logical module names
  paths: {
    'knockout': '//cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min',
    'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min',
    "jqueryui": "//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui",
    "jqueryui-amd": "//cdn.rawgit.com/jquery/jquery-ui/1-11-stable/ui",

    "promise": "//cdn.rawgit.com/components/es6-promise/c95149ffaa2e8162601c57d4282362eac84f929b/promise.min",
    "hammerjs": "//cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.4/hammer.min",
    "ojdnd": "//cdn.rawgit.com/oracle/oraclejet/49fcbbde7c8e178a3a21625d44485536e3ad1aaf/dist/js/libs/dnd-polyfill/dnd-polyfill-1.0.0.min",
    "ojs": "//cdn.rawgit.com/oracle/oraclejet/49fcbbde7c8e178a3a21625d44485536e3ad1aaf/dist/js/libs/oj/debug",
    "ojL10n": "//cdn.rawgit.com/oracle/oraclejet/49fcbbde7c8e178a3a21625d44485536e3ad1aaf/dist/js/libs/oj/ojL10n",
    "ojtranslations": "//cdn.rawgit.com/oracle/oraclejet/49fcbbde7c8e178a3a21625d44485536e3ad1aaf/dist/js/libs/oj/resources",
    "text": "//cdnjs.cloudflare.com/ajax/libs/require-text/2.0.12/text.min",
    "signals": "//cdnjs.cloudflare.com/ajax/libs/js-signals/1.0.0/js-signals.min",
  },
  // Shim configurations for modules that do not expose AMD
  shim: {
    'jqueryui-amd': {
      exports: "$",
      deps: ['jquery']
    },
    'jquery': {
      exports: ['jQuery', '$']
    }
  },
  config: {
    ojL10n: {
      merge: {
        //'ojtranslations/nls/ojtranslations': 'resources/nls/menu'
        // The following addes en-US to the r.js bundle
        //'ojtranslations/nls/ojtranslations': '../../oj/resources/nls/en-US/localeElements'
      }
    }
  }
});

require(['knockout',
  'ojs/ojcore',
  'ojs/ojknockout',
  'promise',
  'ojs/ojdatetimepicker',
  'ojs/ojtable',
  'ojs/ojarraytabledatasource'
], function(ko, oj) {
  'use strict';

  var fakeJAX = function() {
    return new Promise(function(resolve, reject) {
     ...