← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8695: Mobile: use object for storing datavaluesets, not array. Makes it easier to handle duplicate entr...

 

------------------------------------------------------------
revno: 8695
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-10-24 16:44:08 +0200
message:
  Mobile: use object for storing datavaluesets, not array. Makes it easier to handle duplicate entries. Will be used to display old (saved) values when mobile is offline, and the same dvs combination has been saved before.
modified:
  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/index.vm'
--- dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/index.vm	2012-10-24 13:28:42 +0000
+++ dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/index.vm	2012-10-24 14:44:08 +0000
@@ -94,7 +94,7 @@
         $('#offline-status').html(
             _.template(tmpl, {
                 'online': onlineStatus,
-                'dataValueSets': dataValueSets
+                'dvsSize': _.size( dataValueSets )
             })
         );
 
@@ -105,13 +105,13 @@
 <script id="offline-status-template"  type="text/template">
     <h3></h3>
 
-    <% if( dataValueSets.length == 0 ) { %>
+    <% if( dvsSize == 0 ) { %>
         <p>No data is stored locally.</p>
     <% } else { %>
-        <% if( dataValueSets.length == 1 ) { %>
-            <p>You have <strong><%= dataValueSets.length %></strong> form stored locally</p>
+        <% if( dvsSize == 1 ) { %>
+            <p>You have <strong><%= dvsSize %></strong> form stored locally</p>
         <% } else { %>
-            <p>You have <strong><%= dataValueSets.length %></strong> forms stored locally</p>
+            <p>You have <strong><%= dvsSize %></strong> forms stored locally</p>
         <% } %>
     <% } %>
 

=== 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-24 13:28:42 +0000
+++ dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/dhis-web-mobile-resources/js/dhis2.storage.js	2012-10-24 14:44:08 +0000
@@ -80,50 +80,52 @@
     {
         dataValueSets = JSON.parse( dataValueSets );
     } else {
-        dataValueSets = [];
+        dataValueSets = {};
     }
 
     return dataValueSets;
 };
 
+dhis2.storage.makeUploadDataValueSetRequest = function( dataValueSet ) {
+    return $.ajax({
+        url: '../api/dataValueSets',
+        type: 'POST',
+        contentType: 'application/json',
+        data: JSON.stringify( dataValueSet )
+    });
+};
+
+dhis2.storage.getUniqueKey = function( dataValueSet ) {
+    return dataValueSet.orgUnit + '-' + dataValueSet.dataSet + '-' + dataValueSet.period;
+};
+
 dhis2.storage.FormManager.prototype.saveDataValueSet = function( dataValueSet ) {
     var dataValueSets = this.dataValueSets();
 
-    return $.ajax({
-        url: '../api/dataValueSets',
-        type: 'POST',
-        contentType: 'application/json',
-        data: JSON.stringify( dataValueSet )
-    }).error(function() {
+    return dhis2.storage.makeUploadDataValueSetRequest( dataValueSet ).error(function() {
         // add to local dataValueSets
-        dataValueSets.push( dataValueSet );
+
+        // dataValueSets.push( dataValueSet );
+        dataValueSets[dhis2.storage.getUniqueKey(dataValueSet)] = dataValueSet;
+
         localStorage['dataValueSets'] = JSON.stringify( dataValueSets );
     });
 };
 
-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 ) {
-        deferreds.push(dhis2.storage.makeUploadDataValueSetRequest( dataValueSet).success(function() {
-                delete dataValueSets[idx];
+    _.each(dataValueSets, function( value, key ) {
+        deferreds.push(dhis2.storage.makeUploadDataValueSetRequest( value ).success(function() {
+                delete dataValueSets[key];
             })
         );
     });
 
     return $.when.apply( null, deferreds ).always(function() {
         // filter out undefined dataValues (successfully uploaded);
-        dataValueSets = _.filter(dataValueSets, function(dv) { return dv !== undefined; });
+        // dataValueSets = _.filter(dataValueSets, function(dv) { return dv !== undefined; });
         localStorage['dataValueSets'] = JSON.stringify( dataValueSets );
     });
 };