From 0fda7b341258478f91e93398660f8a57f60f64a4 Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Wed, 27 May 2015 16:48:02 -0600 Subject: [PATCH] refactor _pushFeatureCollections --- lib/edit/offlineFeaturesManager.js | 123 ++++++++++++++++------------- 1 file changed, 69 insertions(+), 54 deletions(-) diff --git a/lib/edit/offlineFeaturesManager.js b/lib/edit/offlineFeaturesManager.js index 70c170f..abd62bb 100644 --- a/lib/edit/offlineFeaturesManager.js +++ b/lib/edit/offlineFeaturesManager.js @@ -499,60 +499,7 @@ define([ } } - self._editStore._getFeatureCollections(function(success, result) { - - var featureCollection = - { - featureLayerUrl: layer.url, - featureLayerCollection: layer.toJson() - }; - - // An array of feature collections, of course :-) - var featureCollectionsArray = [ - featureCollection - ]; - - // An object for storing multiple feature collections - var featureCollectionsObject = { - // The id is required because the editsStore keypath - // uses it as a UID for all entries in the database - id: self._editStore.FEATURE_COLLECTION_ID, - featureCollections: featureCollectionsArray - }; - - // If the featureCollections object already exists - if(success){ - var count = 0; - for(var i = 0; i < result.featureCollections.length; i++) { - - // Update the current feature collection - if(result.featureCollections[i].featureLayerUrl === layer.url) { - count++; - result.featureCollections[i] = featureCollection; - } - } - - // If we have a new feature layer then add it to the featureCollections array - if(count === 0) { - result.featureCollections.push(featureCollectionsArray); - } - } - // If empty then we need to add the new featureCollectionsObject - else if(!success && result === null) { - result = featureCollectionsObject; - } - else { - console.error("There was a problem retrieving the featureCollections from editStore."); - } - - // Automatically update the featureCollection store in the database with every ADD, UPDATE - // and DELETE. It can be retrieved via OfflineFeaturesManager.getFeatureCollections(); - self._editStore._pushFeatureCollections(result, function(success, error) { - if(!success){ - console.error("There was a problem creating the featureCollectionObject: " + error); - } - }); - }); + layer._pushFeatureCollections(); // we already pushed the edits into the database, now we let the FeatureLayer to do the local updating of the layer graphics // EDITS_ENQUEUED = callback(true, edit), and EDITS_ENQUEUED_ERROR = callback(false, /*String */ error) @@ -801,6 +748,74 @@ define([ /* internal methods */ + /** + * Automatically creates a set of featureLayerCollections. This is specifically for + * use with offline browser restarts. You can retrieve the collections and use them + * to reconstitute a featureLayer and then redisplay all the associated features. + * + * To retrieve use OfflineFeaturesManager.getFeatureCollections(). + * + * @private + */ + layer._pushFeatureCollections = function(){ + + // First let's see if any collections exists + self._editStore._getFeatureCollections(function(success, result) { + + var featureCollection = + { + featureLayerUrl: layer.url, + featureLayerCollection: layer.toJson() + }; + + // An array of feature collections, of course :-) + var featureCollectionsArray = [ + featureCollection + ]; + + // An object for storing multiple feature collections + var featureCollectionsObject = { + // The id is required because the editsStore keypath + // uses it as a UID for all entries in the database + id: self._editStore.FEATURE_COLLECTION_ID, + featureCollections: featureCollectionsArray + }; + + // If the featureCollectionsObject already exists + if(success){ + var count = 0; + for(var i = 0; i < result.featureCollections.length; i++) { + + // Update the current feature collection + if(result.featureCollections[i].featureLayerUrl === layer.url) { + count++; + result.featureCollections[i] = featureCollection; + } + } + + // If we have a new feature layer then add it to the featureCollections array + if(count === 0) { + result.featureCollections.push(featureCollectionsArray); + } + } + // If it does not exist then we need to add a featureCollectionsObject + else if(!success && result === null) { + result = featureCollectionsObject; + } + else { + console.error("There was a problem retrieving the featureCollections from editStore."); + } + + // Automatically update the featureCollectionsObject in the database with every ADD, UPDATE + // and DELETE. It can be retrieved via OfflineFeaturesManager.getFeatureCollections(); + self._editStore._pushFeatureCollections(result, function(success, error) { + if(!success){ + console.error("There was a problem creating the featureCollectionObject: " + error); + } + }); + }); + }; + /** * Pushes a DELETE request to the database after it's been validated * @param layer