Commits

Anonymous committed 0edd1d6

Backbone-ify server-state deferred and use when running tools in Trackster.

Comments (0)

Files changed (3)

static/scripts/viz/paramamonster.js

           .attr("class", "node")
           .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
   
+        // Set up behavior when node is clicked.
         node.on("click", function(d, i) {
-            console.log(d, i);  
+            console.log(d, i);
+            
+            // Gather: (a) dataset of interest; (b) region(s) of interest and (c) sets of parameters based on node clicked.
+            
+            // Run job by submitting parameters + dataset as job inputs; get dataset ids as result.
+            
+            // Create tracks for all resulting dataset ids.
+            
+            // Display tiles for region(s) of interest.
         });
 
         node.append("circle")

static/scripts/viz/trackster.js

 };
 
 /**
- * Provides support for server-state based deferred. Server is repeatedly polled, and when
- * condition is met, deferred is resolved.
- */
-var server_state_deferred = function(url, url_params, interval, success_fn) {
-    var deferred = $.Deferred(),
-        go = function() {
-            $.getJSON(url, url_params, function(result) {
-                if (success_fn(result)) {
-                    // Result is good, so resolve.
-                    deferred.resolve(result);
-                }
-                else {
-                    // Result not good, try again.
-                    setTimeout(go, interval);
-                }
-            });
-        };
-    go();
-    return deferred;
-};
-
-/**
  * Find browser's requestAnimationFrame method or fallback on a setTimeout 
  */
 var requestAnimationFrame = (function(){
         this.run(url_params, new_track,
                  // Success callback.
                  function(track_data) {
-                     new_track.dataset_id = track_data.dataset_id;
+                     new_track.set_dataset(new Dataset({
+                         id: track_data.dataset_id,
+                         hda_ldda: track_data.hda_ldda
+                     }));
                      new_track.tiles_div.text("Running job.");
                      new_track.init();
                  }
      * Run tool using a set of URL params and a success callback.
      */
     run: function(url_params, new_track, success_callback) {
-        // Add tool params to URL params.
-        $.extend(url_params, this.get_param_values_dict());
+        // Run tool.
+        var ss_deferred = new ServerStateDeferred({
+            url: rerun_tool_url,
+            url_params: $.extend(url_params, this.get_param_values_dict()),
+            interval: 2000,
+            success_fn: function(response) {
+                return response !== "pending";
+            }
+        });
         
-        // Run tool.
-        // TODO: rewrite to use server state deferred.
-        var json_run_tool = function() {
-            $.getJSON(rerun_tool_url, url_params, function(response) {
-                if (response === "no converter") {
-                    // No converter available for input datasets, so cannot run tool.
-                    new_track.container_div.addClass("error");
-                    new_track.content_div.text(DATA_NOCONVERTER);
-                }
-                else if (response.error) {
-                    // General error.
-                    new_track.container_div.addClass("error");
-                    new_track.content_div.text(DATA_CANNOT_RUN_TOOL + response.message);
-                }
-                else if (response === "pending") {
-                    // Converting/indexing input datasets; show message and try again.
-                    new_track.container_div.addClass("pending");
-                    new_track.content_div.text("Converting input data so that it can be used quickly with tool.");
-                    setTimeout(json_run_tool, 2000);
-                }
-                else {
-                    // Job submitted and running.
-                    success_callback(response);
-                }
-            });
-        };
-        json_run_tool();
+        // Start with this status message.
+        //new_track.container_div.addClass("pending");
+        //new_track.content_div.text("Converting input data so that it can be used quickly with tool.");
+        
+        $.when(ss_deferred.go()).then(function(response) {
+            if (response === "no converter") {
+                // No converter available for input datasets, so cannot run tool.
+                new_track.container_div.addClass("error");
+                new_track.content_div.text(DATA_NOCONVERTER);
+            }
+            else if (response.error) {
+                // General error.
+                new_track.container_div.addClass("error");
+                new_track.content_div.text(DATA_CANNOT_RUN_TOOL + response.message);
+            }
+            else {
+                // Job submitted and running.
+                success_callback(response);
+            }            
+        });
     }
 });
 
     init_for_tool_data: function() {
         // Set up track to fetch initial data from raw data URL when the dataset--not the converted datasets--
         // is ready.
-        this.data_url = raw_data_url;
+        this.data_manager.set('data_url', raw_data_url);
         this.data_query_wait = 1000;
         this.dataset_check_url = dataset_state_url;
         
             self.data_query_wait = DEFAULT_DATA_QUERY_WAIT;
 
             // Reset data URL when dataset indexing has completed/when not pending.
-            $.when(
+            var ss_deferred = new ServerStateDeferred({
+                url: self.dataset_state_url,
+                url_params: {dataset_id : self.dataset_id, hda_ldda: self.hda_ldda},
+                interval: self.data_query_wait,
                 // Set up deferred to check dataset state until it is not pending.
-                server_state_deferred(self.dataset_state_url,
-                                      {dataset_id : self.dataset_id, hda_ldda: self.hda_ldda},
-                                      self.data_query_wait,
-                                      function(result) { return result !== "pending" })
-            ).then(function() {
+                success_fn: function(result) { return result !== "pending" }
+            });
+            $.when(ss_deferred.go()).then(function() {
                 // Dataset is indexed, so use default data URL.
-                self.data_url = default_data_url;
+                self.data_manager.set('data_url', default_data_url);
             });
                         
             // Reset post-draw actions function.
     this.set_painter_from_config();
 };
 extend(FeatureTrack.prototype, Drawable.prototype, TiledTrack.prototype, {
+    set_dataset: function(dataset) {
+        this.dataset_id = dataset.get('id');
+        this.hda_ldda = dataset.get('hda_ldda');
+        this.data_manager.set('dataset', dataset);
+    },
+    
     set_painter_from_config: function() {
         if ( this.config.values['connector_style'] === 'arcs' ) {
             this.painter = painters.ArcLinkedFeaturePainter;

static/scripts/viz/visualization.js

 // --------- Models ---------
 
 /**
+ * Implementation of a server-state based deferred. Server is repeatedly polled, and when
+ * condition is met, deferred is resolved.
+  */
+var ServerStateDeferred = Backbone.Model.extend({
+    defaults: {
+        url: null,
+        url_params: {},
+        interval: 1000,
+        success_fn: function(result) { return true; }
+    },
+    
+    /**
+     * Returns a deferred that resolves when success function returns true.
+     */
+    go: function() {
+        var deferred = $.Deferred(),
+            self = this,
+            success_fn = self.get('success_fn'),
+            interval = self.get('interval'),
+             _go = function() {
+                 $.getJSON(self.get('url'), self.get('url_params'), function(result) {
+                     if (success_fn(result)) {
+                         // Result is good, so resolve.
+                         deferred.resolve(result);
+                     }
+                     else {
+                         // Result not good, try again.
+                         setTimeout(_go, interval);
+                     }
+                 });
+             };
+         _go();
+         return deferred;
+    }
+});
+
+/**
  * Generic cache that handles key/value pairs.
  */ 
 var Cache = Backbone.Model.extend({