← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8603: mobile: $.Deferred approach to chaining. Will be refined later.

 

------------------------------------------------------------
revno: 8603
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-10-20 00:31:07 +0200
message:
  mobile: $.Deferred approach to chaining. Will be refined later.
modified:
  dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/base.vm
  dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/index.vm
  dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/dhis-web-mobile-resources/js/dhis2.storage.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/base.vm'
--- dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/base.vm	2012-10-19 10:23:18 +0000
+++ dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/base.vm	2012-10-19 22:31:07 +0000
@@ -1,6 +1,6 @@
 <!DOCTYPE html>
 <html manifest="../dhis-web-mobile-resources/manifest.appcache">
-<!-- <html> -->
+<!--  <html> -->
 <head>
 	<title>DHIS2</title>
 	<meta name="viewport" content="width=device-width, initial-scale=1">

=== modified file 'dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/index.vm'
--- dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/index.vm	2012-10-19 12:24:40 +0000
+++ dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/index.vm	2012-10-19 22:31:07 +0000
@@ -1,43 +1,42 @@
 
 <script>
-    $.ajax({
-        url: '../mobile',
-        async: false
-    }).complete(function(e) {
-        if( e.getResponseHeader('Login-Page') ) {
-            document.location = '../mobile';
-        }
-    });
-
-    $(document).bind('pagebeforecreate',function(){
-        $.mobile.showPageLoadingMsg();
-
-        $.ajax({
+    redirectToLoginIfRequired();
+
+    function redirectToLoginIfRequired() {
+        return $.ajax({
+            url: '../mobile',
+            async: false
+        }).complete(function(e) {
+            if( e.getResponseHeader('Login-Page') ) {
+                document.location = '../mobile';
+            }
+        });
+    }
+
+    function getDashboard() {
+        return $.ajax({
             url: '../api/currentUser/dashboard',
             dataType: 'json'
         }).success(function(data) {
             if( data.unreadMessageConversation > 0 ) {
                 $('#messages a').append("<span class='ui-li-count'>" + data.unreadMessageConversation + "</span>");
             }
-
-            fm.initialize({
-                success: function() {
-                    fm.uploadDataValueSets({
-                        success: function() {
-                        },
-                        done: function() {
-                            $.mobile.hidePageLoadingMsg();
-                        }
-                    });
-                }
-            });
         }).error(function() {
             $('#messages a').append("<span class='ui-li-count'>Offline</span>")
             $.mobile.hidePageLoadingMsg();
-        }).complete(function() {
+        });
+    }
+
+    $(document).bind('pagebeforecreate',function(){
+        $.mobile.showPageLoadingMsg();
+
+        getDashboard().then(
+                fm.getMetaData().then(
+                        fm.uploadDataValueSets()).always(function() {
             $('section[data-role="content"] ul').listview('refresh');
-        });
-    });
+            $.mobile.hidePageLoadingMsg();
+        }));
+    })
 </script>
 
 <section data-role="page" id="index-page" data-theme="c">

=== modified file 'dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/dhis-web-mobile-resources/js/dhis2.storage.js'
--- dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/dhis-web-mobile-resources/js/dhis2.storage.js	2012-10-19 18:54:09 +0000
+++ dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/dhis-web-mobile-resources/js/dhis2.storage.js	2012-10-19 22:31:07 +0000
@@ -34,28 +34,15 @@
     this._id = _.uniqueId('formManager');
 };
 
-dhis2.storage.FormManager.prototype.initialize = function ( args ) {
-    $.ajax({
+dhis2.storage.FormManager.prototype.getMetaData = function () {
+    console.log('get metadata');
+
+    return $.ajax({
         url      : '../api/currentUser/forms',
         dataType : 'json'
-    }).success(function ( data ) {
+    }).done(function ( data ) {
         localStorage['organisationUnits'] = JSON.stringify(data.organisationUnits);
         localStorage['forms'] = JSON.stringify(data.forms);
-
-        if(args.success) {
-            args.success.call(null, data);
-        }
-    }).error(function () {
-        // offline ? reuse meta-data already present
-            console.log("unable to load meta-data");
-
-        if(args.error) {
-            args.error.call();
-        }
-    }).complete(function() {
-        if(args.complete) {
-            args.complete.call();
-        }
     });
 };
 
@@ -119,34 +106,31 @@
     });
 };
 
-dhis2.storage.FormManager.prototype.uploadDataValueSets = function( args ) {
+dhis2.storage.makeUploadDataValueSetRequest = function( dataValueSet ) {
+    return $.ajax({
+        url: '../api/dataValueSets',
+        type: 'POST',
+        contentType: 'application/json',
+        data: JSON.stringify( dataValueSet )
+    });
+};
+
+dhis2.storage.FormManager.prototype.uploadDataValueSets = function() {
     var dataValueSets = this.dataValueSets();
+    var deferreds = [];
 
     _.each(dataValueSets, function( dataValueSet, idx ) {
-        $.ajax({
-            url: '../api/dataValueSets',
-            type: 'POST',
-            contentType: 'application/json',
-            data: JSON.stringify(dataValueSet),
-            async: false
-        }).success(function() {
-            delete dataValueSets[idx];
-
-            if( args.success ) {
-                args.success.call();
-            }
-        }).error(function() {
-        });
-    });
-
-    // filter out undefined dataValues (successfully uploaded);
-    dataValueSets = _.filter(dataValueSets, function(dv) { return dv !== undefined; });
-
-    localStorage['dataValueSets'] = JSON.stringify( dataValueSets );
-
-    if( args.done) {
-        args.done.call();
-    }
+        deferreds.push(dhis2.storage.makeUploadDataValueSetRequest( dataValueSet).success(function() {
+                delete dataValueSets[idx];
+            })
+        );
+    });
+
+    return $.when.apply( null, deferreds ).always(function() {
+        // filter out undefined dataValues (successfully uploaded);
+        dataValueSets = _.filter(dataValueSets, function(dv) { return dv !== undefined; });
+        localStorage['dataValueSets'] = JSON.stringify( dataValueSets );
+    });
 };
 
 // global storage manager instance