This project is read-only.
The following sample shows how to populate a combo box using the caching API.

<!DOCTYPE html>
  <title>Combo Box Sample</title>
  <script type="text/javascript" src="datajs-0.0.3.js"></script>

<select id='targetbox'></select>

<script type="text/javascript">

function populateComboBox(cache, id, propertyName) {
  var chunkSize = 100;
  var element = document.getElementById(id);
  var index = 0;

  var addItem = function (text) {
    var option = document.createElement("OPTION");
    option.text = text;
    element.add(option, null);

  var successCallback = function (data) {
    for (var i = 0; i < data.length; i++) {

    if (data.length === chunkSize) {
      index += chunkSize;
      cache.readRange(index, chunkSize).then(successCallback, errorCallback);
    } else {
      element.disabled = false;

  var errorCallback = function (err) {
    while (element.options.length) {
    addItem("(error - " + err.message + ")");

  element.disabled = true;
  cache.readRange(index, chunkSize).then(successCallback, errorCallback);

var cache = datajs.createDataCache({
  name: "genres",
  source: "",
  enableJsonpCallback: true

populateComboBox(cache, "targetbox", "Name");



The page just creates a cache with a source on a different domain using JSONP, and then invokes the populateComboBox helper function with the ID of the target element and the property name to be used to fill in the combo box.

The helper function works by reading data from the cache in chunks. You can try playing with the chunkSize value to see what works best for you. It also disables and then re-enables the combo box while data is loading, but you can try other things: fade out / in, start hidden and then slide in, show an animation GIF while loading, etc.

If you run this in your favorite browser you should see a much faster load time the second time, courtesy of the cache.

Last edited Jun 8, 2011 at 6:35 AM by marcelolr, version 1


No comments yet.