Dojo Offline Toolkit in Code

Written by on January 23rd, 2007 in Ajax News.

Brad Neuberg keeps it moving with the Dojo Offline Toolkit API. His latest work details the early API via example. The example is a web based email system, and the code using the API is below (and shows you how you would use it).

The interesting news is that you can start work even without the local proxy that was mentioned before, as long as you set the right headers for development.

JAVASCRIPT:

  1.  
  2.  
  3. // bring in Dojo Offline
  4. dojo.require(”offline.*”);
  5.  
  6. // indicate what files we want offline, such
  7. // as our application’s images, HTML, CSS,
  8. // JavaScript, etc.
  9. offline.files.put(
  10.                   [”/images/toolbar.gif”,
  11.                   “index.html”,
  12.                   “/css/global.css”,
  13.                   “dojo.js”
  14.                   ]);
  15.  
  16. // define our application’s name; this will be used to
  17. // customize the default Offline Toolkit UI
  18. offline.ui.appName = “Web Outlook”;
  19.  
  20. // define what elements we would like disabled when we go offline;
  21. // these will be re-enabled when we go online                  
  22. offline.ui.disableElementsOffline(
  23.                                   [”configurationLink”,
  24.                                   “searchField”
  25.                                   ]);
  26.  
  27. var emails, contacts, tasks;
  28.  
  29. // called when the page is finished loading and the offline toolkit
  30. // is ready to be used
  31. offline.onLoad = function(){
  32.    // define where to put our offline user data; when
  33.    // we sync with the server, each kind of data will have
  34.    // an itemType, such as "emails", "tasks", etc. These
  35.    // data structures will automatically be kept in sync
  36.    // so we can use them in our application, and will
  37.    // automatically be persisted in local client-side
  38.    // storage
  39.    emails = offline.getDataStore(”email”);
  40.    contacts = offline.getDataStore(”contact”);
  41.    tasks = offline.getDataStore(”task”);
  42. }
  43.  
  44. // default implementation of offline.onOffline and offline.onOnline
  45. // will simply use the disableElementsOffline values
  46. // to enable and disable these elements, and do an automatic synchronization inside of onOnline
  47.  
  48. function addContact(contact){
  49.    contacts.newItem(contact);
  50. }
  51.  
  52. function displayEmails(){
  53.    var displayMe = null;
  54.  
  55.    if(offline.isOnline() == false){
  56.       displayMe = emails.find().items;
  57.    }else{
  58.       displayMe = // …remotely fetch emails
  59.    }
  60.  
  61.    // do something with array of emails
  62. }
  63.  
  64. function deleteTask(task){
  65.    tasks.deleteItem(task);
  66. }
  67.  
  68. function sendEmail(email){
  69.    if(offline.isOnline == true){
  70.       // send this email to the server
  71.    }else{
  72.       // else we are offline; just queue this email up
  73.  
  74.       // store it’s value in our offline cache
  75.       emails.newItem(email);
  76.  
  77.       // now create a custom sync log entry for a ’send’ command
  78.       var c = new offline.sync.Command();
  79.       c.name = “send”;
  80.       c.itemType = “email”;
  81.       c.item = email;
  82.  
  83.       offline.sync.log.add(c);
  84.  
  85.       // later, when we sync, this command will be replayed to the
  86.       // server for the email to be sent
  87. }
  88.  

Source: Ajaxian
Original Article: http://ajaxian.com/archives/dojo-offline-toolkit-in-code

Leave a Reply

You must be logged in to post a comment.



Site Navigation