//App.Store = DS.Store.extend({
//  revision: 12,
//  adapter: DS.RESTAdapter.create({
//    bulkCommit: false
//  })
//});

//window.attr = DS.attr;
App.CompoundAdapter = DS.Adapter.extend({
  find: function(store, type, id) {
    // return a promise inside of which is the callback which either resolves with the retrieved data or rejects with the status
    var promise = new Ember.RSVP.Promise(function(resolve, reject){
      var searcher = new Openphacts.CompoundSearch(ldaBaseUrl, appID, appKey);
      var pathwaysSearcher = new Openphacts.PathwaySearch(ldaBaseUrl, appID, appKey);
      // get the compound details  
	  var callback=function(success, status, response){  
        if (success) {
            //get the pathways for the compound
            var pathwaysByCompoundCallback=function(success, status, response){
              if (success && response) {
                  var pathwayResults = pathwaysSearcher.parseByCompoundResponse(response);
                  $.each(pathwayResults, function(index, pathwayResult) {
                    pathwayID = pathwayResult.identifier.split('/').pop();
                    //load the ids of the pathways, the compound model will lazy load it
                    compoundResult['pathways'].push(pathwayID);
                    //me.store.find('pathway', pathwayID).then(function(pathway) {
                    //  thisCompound.get('pathways').pushObject(pathway);
                    //});
                  });
                  // all data added so send it back
                  resolve(compoundResult);
              }
            }
	        var compoundResult = searcher.parseCompoundResponse(response);
            compoundResult['pathways'] = [];
            pathwaysSearcher.byCompound('http://www.conceptwiki.org/concept/' + id, null, null, 1, 50, null, pathwaysByCompoundCallback);
        } else {
            reject(status);
        }
      }
	  searcher.fetchCompound('http://www.conceptwiki.org/concept/' + id, null, callback);
    });
    return promise;
  }
});
App.TargetAdapter = DS.Adapter.extend({
  find: function(store, type, id) {
    return new Ember.RSVP.Promise(function(resolve, reject){
      var searcher = new Openphacts.TargetSearch(ldaBaseUrl, appID, appKey);  
	  var callback=function(success, status, response){  
        if (success) {
	        var targetResult = searcher.parseTargetResponse(response); 
            resolve(targetResult);
        } else {
            reject(status);
        }
      }
	  searcher.fetchTarget('http://www.conceptwiki.org/concept/' + id, null, callback);
    });
  }
});
App.PathwayAdapter = DS.Adapter.extend({
  find: function(store, type, id) {
    console.log('pathway adapter find');
    var identifier = id;
    return new Ember.RSVP.Promise(function(resolve, reject){
      var searcher = new Openphacts.PathwaySearch(ldaBaseUrl, appID, appKey);
      var pathwayInfoCallback=function(success, status, response){
        if (success && response) {
            // we have the pathway so now  find all the compounds and add ids for lazy loading
            var getCompoundsCallback=function(success, status, response){
              if (success && response) {
                  var compoundsResult = searcher.parseGetCompoundsResponse(response);
                  $.each(compoundsResult.metabolites, function(index, metabolite) {
                    //load the ids of the pathways, the compound model will lazy load it
                    // need to then find the cw id using the mapURL function
                    pathwayResult['compounds'].push(metabolite);
                  });
                  // all data added so send it back
                  resolve(pathwayResult);
              }
            }
          var pathwayResult = searcher.parseInformationResponse(response);
          pathwayResult['id'] = identifier;
          pathwayResult['compounds'] = [];
          searcher.getCompounds('http://identifiers.org/wikipathways/' + id, null, getCompoundsCallback);
        } else {
          reject(status);
        }
      };
      searcher.information('http://identifiers.org/wikipathways/' + id, null, pathwayInfoCallback);
    });
  }
});

@@ -318,16 +318,25 @@
 trieved 
+compound 
 data or 
@@ -702,32 +702,33 @@
 (success) %7B%0A
+%09
         
     //get th
@@ -719,54 +719,484 @@
     
-    //get the pathways for the compound%0A      
+var compoundResult = searcher.parseCompoundResponse(response);%0A            compoundResult%5B'pathways'%5D = %5B%5D;%0A            resolve(compoundResult);%0A        %7D else %7B%0A            reject(status);%0A        %7D%0A      %7D%0A%09  searcher.fetchCompound('http://www.conceptwiki.org/concept/' + id, null, callback);%0A    %7D);%0A    // after finding the compound, find the pathways%0A    promise.then(function(data) %7B%0A      var pathwaysSearcher = new Openphacts.PathwaySearch(ldaBaseUrl, appID, appKey);%0A
     
@@ -1265,38 +1265,32 @@
 ponse)%7B%0A        
-  
-    
 if (success && r
@@ -1308,30 +1308,24 @@
             
-     
- 
 var pathwayR
@@ -1393,30 +1393,24 @@
             
-      
 $.each(pathw
@@ -1463,30 +1463,24 @@
             
- 
-     
 pathwayID = 
@@ -1540,301 +1540,161 @@
     
-      //load the ids of the pathways, the compound model will lazy load it%0A                    compoundResult%5B'pathways'%5D.push(pathwayID);%0A                    //me.store.find('pathway', pathwayID).then(function(pathway) %7B%0A                    //  thisCompound.get('pathways').pushObject(pathway
+//have to find the pathway record and add it, just adding the ID does not work%0A              data%5B'pathways'%5D.pushObject(store.find('pathway', pathwayID)
 );%0A 
@@ -1704,26 +1704,16 @@
         
-  
-      //
 %7D);%0A    
@@ -1720,550 +1720,167 @@
     
-          %7D);%0A                  // all data added so send it back%0A                  resolve(compoundResult);%0A              %7D%0A            %7D%0A%09        var compoundResult = searcher.parseCompoundResponse(response);%0A            compoundResult%5B'pathways'%5D = %5B%5D;%0A            pathwaysSearcher.byCompound('http://www.conceptwiki.org/concept/' + id, null, null, 1, 50, null, pathwaysByCompoundCallback);%0A        %7D else %7B%0A            reject(status);%0A        %7D%0A      %7D%0A%09  searcher.fetchCompound('http://www.conceptwiki.org/concept/' + id, null, callback)
+%7D%0A      %7D%0A      pathwaysSearcher.byCompound('http://www.conceptwiki.org/concept/' + id, null, null, 1, 50, null, pathwaysByCompoundCallback);%0A      return data
 ;%0A  

load pathways for a compound using promises in the compound adapter