//
// ========================================================
// Copyright (c) 2012 Whamcloud, Inc.  All rights reserved.
// ========================================================


var ApiCache = function(){
  var Filesystem = Backbone.Model.extend({
    urlRoot: "/api/filesystem/"
  });

  var FilesystemCollection = Backbone.Collection.extend({
    model: Filesystem,
    url: "/api/filesystem/"
  });

  var Target = Backbone.Model.extend({
    urlRoot: "/api/filesystem/"
  });

  var TargetCollection = Backbone.Collection.extend({
    model: Target,
    url: "/api/target/"
  });

  var filesystem_collection = new FilesystemCollection();
  var target_collection = new TargetCollection();

  var init = function(params) {
    params = params || {};

    var collections = [filesystem_collection, target_collection]
    var init_counter = 0;
    _.each(collections, function(collection) {
      collection.fetch({success: function() {
        init_counter = init_counter + 1;
        if (init_counter == collections.length) {
          if (params.success) {
            params.success();
          }
        }
      }});
    });
  };

  function get(obj_type, obj_id) {
    var collection = undefined;
    if (obj_type == 'target') {
      collection = target_collection;
    } else if (obj_type == 'filesystem') {
      collection = filesystem_collection;
    }

    var object = collection.get(obj_id);
    if (!object) {
      collection.fetch({add:true, data: {id: obj_id}});
    }
    return object;
  }

  return {
    filesystem: filesystem_collection,
    target: target_collection,
    get: get,
    init: init
  }
}();

@@ -591,27 +591,16 @@
 %0A%0A  var 
-filesystem_
 collecti
@@ -597,26 +597,47 @@
 r collection
+s
  =
+ %7B%0A    'filesystem':
  new Filesys
@@ -655,35 +655,23 @@
 on()
-;%0A  var 
+,%0A    '
 target
-_collection =
+':
  new
@@ -693,135 +693,150 @@
 on()
-;%0A%0A  var init = function(params) %7B%0A    params = params %7C%7C %7B%7D;%0A%0A    var collections = %5Bfilesystem_collection, target_collection%5D
+%0A  %7D;%0A%0A  var outstanding_requests = %7B%0A    'filesystem': %5B%5D,%0A    'target': %5B%5D%0A  %7D;%0A%0A  var init = function(params) %7B%0A    params = params %7C%7C %7B%7D;%0A
 %0A   
@@ -1211,407 +1211,556 @@
 n = 
-undefined;%0A    if (obj_type == 'target') %7B%0A      collection = target_collection;%0A    %7D else
+collections%5Bobj_type%5D;%0A%0A    var object = collection.get(obj_id);%0A   
  if (
+!
 obj
-_type == 'filesystem') %7B%0A      collection = filesystem_collection
+ect) %7B%0A      if (_.include(outstanding_requests%5Bobj_type%5D, obj_id)) %7B%0A        return null
 ;%0A    
-%7D%0A%0A    var object = collection.get(obj_id);%0A    if (!object) %7B%0A      collection.fetch(%7Badd:true,
+  %7D else %7B%0A        outstanding_requests%5Bobj_type%5D.push(obj_id);%0A        collection.fetch(%7B%0A            add:true,%0A           
  data:
- 
 %7Bid:
- 
 obj_id%7D
-%7D);
+,
 %0A    
-%7D%0A
+ 
     
-return object;%0A  %7D%0A%0A  return %7B%0A    filesystem: filesystem_collection,%0A    target: target_collection,
+   success:function () %7B%0A              outstanding_requests%5Bobj_type%5D = _.reject(outstanding_requests%5Bobj_type%5D, function (x) %7Breturn x == obj_id;%7D);%0A            %7D%0A        %7D);%0A      %7D%0A    %7D%0A    return object;%0A  %7D%0A%0A  return %7B
 %0A   

Fix exception retreiving chart legend objects