← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12429: rewrite of deferred chains in anonymous entry, wip

 

------------------------------------------------------------
revno: 12429
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-10-04 14:54:12 +0200
message:
  rewrite of deferred chains in anonymous entry, wip
modified:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.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-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2013-10-03 04:32:03 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2013-10-04 12:54:12 +0000
@@ -70,54 +70,75 @@
         return;
     }
 
-    var deferred1 = $.Deferred();
-    var deferred2 = $.Deferred();
-    var promise = deferred2.promise();
-
-    _.each( metaData.optionSets, function ( item, idx ) {
-        DAO.store.get('optionSets', item.uid).done(function(obj) {
-            if(!obj || obj.optionSet.version !== item.v) {
-                promise = promise.then(function() {
-                    return $.ajax({
-                        url: 'getOptionSet.action',
-                        data: {
-                            id: item.uid
-                        },
-                        dataType: 'json',
-                        cache: false
-                    }).done(function(data) {
-                        var obj = {};
-                        obj.id = item.uid;
-                        obj.optionSet = data.optionSet;
-                        DAO.store.set('optionSets', obj);
-                    });
-                });
-            }
+    var def = $.Deferred();
+    var promise = def.promise();
+
+    var builder = $.Deferred();
+    var build = builder.promise();
+
+    _.each( metaData.optionSets, function ( item ) {
+        build = build.then(function() {
+            var d = $.Deferred();
+            var p = d.promise();
+            DAO.store.get('optionSets', item.uid).done(function(obj) {
+                if(!obj || obj.optionSet.version !== item.v) {
+                    promise = promise.then( makeOptionSetRequest(item.uid) );
+                }
+
+                d.resolve();
+            });
+
+            return p;
         });
     } );
 
     if ( metaData.usernames ) {
-        promise = promise.then( function () {
-            return $.ajax( {
-                url: 'getUsernames.action',
-                dataType: 'json',
-                cache: false
-            } ).done( function ( data ) {
-                    var obj = {};
-                    obj.id = 'usernames';
-                    obj.usernames = data.usernames;
-                    DAO.store.set( 'usernames', obj );
-                } )
+        promise = promise.then( makeUsernameRequest() );
+    }
+
+    build.done(function() {
+        def.resolve( metaData );
+
+        promise = promise.done( function () {
+            console.log(promise);
+            console.log('done');
         } );
-    }
-
-    promise = promise.then( function () {
-        deferred1.resolve( metaData );
-    } );
-
-    deferred2.resolve();
-
-    return deferred1.promise();
+    });
+
+    builder.resolve();
+
+    return promise;
+}
+
+function makeUsernameRequest() {
+    return function() {
+        return $.ajax( {
+            url: 'getUsernames.action',
+            dataType: 'json',
+            cache: false
+        }).done(function( data ) {
+            var obj = {};
+            obj.id = 'usernames';
+            obj.usernames = data.usernames;
+            DAO.store.set( 'usernames', obj );
+        });
+    }
+}
+
+function makeOptionSetRequest( id ) {
+    return function() {
+        return $.ajax({
+            url: 'getOptionSet.action',
+            data: { id: id },
+            dataType: 'json',
+            cache: false
+        }).done(function(data) {
+            var obj = {};
+            obj.id = id;
+            obj.optionSet = data.optionSet;
+            DAO.store.set('optionSets', obj);
+        });
+    }
 }
 
 function updateOfflineEvents() {
@@ -231,9 +252,12 @@
         cache: false
     } );
 
+    $("#programId").attr('disabled', true);
+
     $( "#orgUnitTree" ).one( "ouwtLoaded", function () {
         var def = $.Deferred();
         var promise = def.promise();
+
         promise = promise.then( DAO.store.open );
         promise = promise.then( loadPrograms );
         promise = promise.then( loadProgramStages );
@@ -241,6 +265,8 @@
         promise = promise.then( updateOfflineEvents );
         promise = promise.then( checkOfflineData );
         promise = promise.then( function () {
+            $("#programId").removeAttr('disabled');
+
             selection.setListenerFunction( organisationUnitSelected );
 
             dhis2.availability.startAvailabilityCheck();