[DataCache] Updated Examples

Again the examples seem out of date. I'll update the remaining examples.  Sorry for not consolidating all of them into one email!

- 4.1.1. Examples
http://dev.w3.org/2006/webapi/DataCache/#examples
The second example seems to be out of date for a number of reasons.

[[
  var uri = ...
  var cache = window.openDataCache();
  var local = function(request, response) {
    response.statusCode = 200;
    response.statusLine = 'HTTP/1.1 OK';
    response.bodyText = ...
    response.send();
  };

  window.navigator.registerOfflineHandler(uri, local);

  var txn = cache.offlineTransaction(function(txn) {
    txn.capture(uri, '', null, ['GET']);
    txn.commit();
  });
]]

The issues I see are:

  - cache.offlineTransaction does not return a value
  - there is no commit() in an offlineTransaction
  - response.X = ...; is not valid, use the IDL's setX functions
  - statusCode and statusLine/Text are both set with the IDL's setStatus()

I would have expected the example to look more like the following:

----
  var uri = ...
  var cache = window.openDataCache();
  var local = function(request, response) {
    response.setStatus(200, 'HTTP/1.1 OK');
    repsonse.setResponseText(...);
    response.send();
  };

  window.navigator.registerOfflineHandler(uri, local);

  cache.offlineTransaction(function(txn) {
    txn.capture(uri, '', null, ['GET']);
  });
----

Also "localServer", "interceptor", or "intercept" (used in a later example) may be better names than "local" for the interceptor function. But that is more of a style issue.



- 4.1.1. Examples
http://dev.w3.org/2006/webapi/DataCache/#examples
The third example seems to be out of date for a number of reasons.

[[
  cache = window.openDataCache();
  var intercept = function(request, response) {
    if (...) {
      // validation fails
      response.setStatus(400, 'HTTP/1.1 Bad Request');
      response.send();
      return;
    }
    cache.offlineTransaction(function(txn) {
      txn.oncommitted = function() {
        response.bodyText = request.bodyText;
        response.headers['Content-Type'] = type;
        response.statusCode = 200;
        response.statusLine = 'HTTP/1.1 OK';
        response.send();
      });
      txn.capture(request.targetURL, request.bodyText, request.headers['Content-Type']);
      txn.commit();
    });
  };

  var review = function(request, response) {
    cache.offlineTransaction(function(txn) {
      txn.capture(request.targetURL, response.bodyText, response.headers['Content-Type']);
      txn.commit();
    });
  };

  window.navigator.registerOfflineHandler(uri, intercept, review);
]]


The issues I see are:

  - use `var` for the cache variable in the Window's scope
  - `uri` variable is not declared in the Window's scope, only the Worker's
  - `type` variable is not declared at all, I'll leave that blank, as I don't understand the example's intent
  - there is no commit() in an offlineTransaction
  - response.X = ...; is not valid, use the IDL's setX functions (like at the top)
  - request.targetURL should be request.target
  - the surrounding text says the interceptor is called on PUT, but no dynamic method is provided

I would have expected the example to look more like the following:

----
  var uri = ...
  var cache = window.openDataCache();
  var intercept = function(request, response) {
    if (...) {
      // validation fails
      response.setStatus(400, 'HTTP/1.1 Bad Request');
      response.send();
      return;
    }
    cache.offlineTransaction(function(txn) {
      txn.oncommitted = function() {
        response.setStatus(200, 'HTTP/1.1 OK');
        response.setResponseText(request.bodyText);
        response.setResponseHeader('Content-Type', ...);
        response.send();
      });
      txn.capture(request.target, request.bodyText, request.headers['Content-Type'], ['PUT']);
    });
  };

  var review = function(request, response) {
    cache.offlineTransaction(function(txn) {
      txn.capture(request.target, response.bodyText, response.headers['Content-Type'], ['PUT']);
    });
  };

  window.navigator.registerOfflineHandler(uri, intercept, review);
----

Received on Thursday, 10 December 2009 21:02:56 UTC