11 Replies Latest reply: May 25, 2012 2:05 PM by BC_Sothebys RSS

    Pass in another variable thru onFolioData()

    BC_Sothebys

      This is my first time dealing with Adobe DPS and I am trying to pass another variable into the onFolioData function.

       

      Right now my code looks like so:

       

       

      var init_dps_api = function(productIDs, saleNumbers) {
          var onFolioData = function (data) {
              var len = data.length;
      
              // The library returns an array of folio descriptor objects.  Put them into a hash indexed by something more convenient (ie productId)
              for (var i = 0; i < len; i++) {
                  folioDataHash[data[i].productId,data[i].saleNumber] = data[i];
              }
      
              var folio;
              for (var j=0; j<productIDs.length; j++){
      
                      folio = folioDataHash[productIDs[j],saleNumbers[j]];
      
                      if (folio) {
                          //adobe.dps.store.getPreviewImage(productIDs[j], true, 768, 1024, onPreviewImage2(productIDs[j]));
                          adobe.dps.store.getPreviewImage(productIDs[j], true, 768, 1024, onPreviewImage(productIDs[j],saleNumbers[j]));
      
                      }
                      else {
                          //console("The productId you have defined for purchase/download does not exist within your Fulfillment account");
                      }
              }
          };
      
          var onPreviewImage = function(productID,saleNumber) {      
              var s2= "";
              var folio2 = folioDataHash[productID,saleNumber];
      ........
      
           }
      }
      
      

       

      In the above code you can see that I want to pass in the saleNumber, but it is breaking the page. Is there a way to do this? Can someone from the Adobe team please assist?

       

      Reason for this is that from our jsp pages, we need to pass salenumbers for our Site Catalyst tracking, so I am grabbing the salenumber from the jsp page and passing it via the javascript api to trigger an Omniture function.

       

      Thank you for your time and patience.

       

      B

       

      Message was edited by: BC_Sothebys

        • 1. Re: Pass in another variable thru onFolioData()
          BC_Sothebys Community Member

          Does the api only allow you to pass one variable?

           

          Let me also add that this is a storefront I am trying to create/customize, so any help would be great.

          • 2. Re: Pass in another variable thru onFolioData()
            Bob Bringhurst - Adobe Adobe Employee

            This user forum isn't the best place for advanced enterprise customization questions. I'll try to find someone to help you.

            • 3. Re: Pass in another variable thru onFolioData()
              BC_Sothebys Community Member

              Thank you Bob. I really appreciate it.

              • 4. Re: Pass in another variable thru onFolioData()
                derek_lu Community Member

                I am assuming you are looking at the callback for getFolioData(). The only parameter that gets passed is an array of folios. What other variable are you looking for? Is it a scoping issue where your variable is not available in the callback? If that is the case you could do something like this:

                 

                var scope = this;

                adobe.dps.store.getFolioData(function(data){ scope.onFolioData(data) });

                 

                Also, getPreviewImage() does not generate unique callbacks. So you have to wait for one callback to execute before doing another one otherwise the prior one will get overwritten by the latter one.

                • 5. Re: Pass in another variable thru onFolioData()
                  BC_Sothebys Community Member

                  In the main jsp file, it is calling multiple publication ids from the cms. So let's say that on the page, there are 5 buckets. In each bucket, there are 5 publication ids. Each of the publication ids are being pushed into a publicationIDs array.

                   

                  <script>
                    publicationIDs.push('<%=publicationId%>');
                  </script>
                  

                   

                  This array then gets called in the api

                   

                  var init_dps_api = function(productIDs) {
                      var folioDataHash = {};
                  
                      var onFolioData = function (data) {
                          var len = data.length;
                          
                          for (var i = 0; i < len; i++) {
                              folioDataHash[data[i].productId] = data[i];
                          }
                  
                          var folio;
                          for (var j=0; j<productIDs.length; j++){
                                  folio = folioDataHash[productIDs[j]];
                                  
                                  if (folio) {
                                      adobe.dps.store.getPreviewImage(productIDs[j], true, 768, 1024, onPreviewImage(productIDs[j]));
                                  }
                                  else {
                                      console("The productId you have defined for purchase/download does not exist within your Fulfillment account");
                                  }
                          }
                      };
                  
                      var onPreviewImage = function(productID) {      
                          var s2= "";
                          var node2 = "";
                          var folio2 = folioDataHash[productID];
                          if (!folio2) return;  // un-known folio...nothing to show.
                  
                          if(globalTab == 'featured') {
                              node2 = document.getElementById(productID + "_publicationId_Holder_fc");
                              
                              if(isLoggedIn) {
                                  s2 += "<div class='buyButton' id='" + productID + "'>";
                                  
                                  if (folio2.state=='200') {
                                      s2 +="Download";
                                  } else {
                                      s2 += "View";
                                  }
                                  
                                  s2 += "</div>";
                                  
                                  if (node2) {
                                      node2.innerHTML = s2;
                                      node2.onclick=function(){
                                          buy(productID);
                                      };
                                  }       
                                  
                              } else {
                                  s2 += "<div class='buyButtonGreyed' id='" + productID + "'>";
                                  s2 += "Register to Download";
                                  s2 += "</div>";
                                  
                                  if (node2) {
                                      node2.innerHTML = s2;
                                  
                                      node2.onclick=function(){
                                          showMsgBox1();
                                      };
                                  }
                              }
                          }
                          
                          if(globalTab == 'view-all') {
                              node2 = document.getElementById(productID + "_publicationId_Holder_va");
                              
                              if(isLoggedIn) {
                                  s2 += "<div class='buyButton' id='" + productID + "'>";
                                  
                                  if (folio2.state=='200') {
                                      s2 +="Download";
                                  } else {
                                      s2 += "View";
                                  }
                  
                                  s2 += "</div>";
                                  
                                  if (node2) {
                                      node2.innerHTML = s2;
                                      node2.onclick=function(){
                                          buy(productID);
                                      };
                                  }       
                                  
                              } else {
                                  s2 += "<div class='buyButtonGreyed' id='" + productID + "'>";
                                  s2 += "Register to Download";
                                  s2 += "</div>";
                                  
                                  if (node2) {
                                      node2.innerHTML = s2;
                                      
                                      node2.onclick=function(){
                                          showMsgBox();
                                      };
                                  }
                              }
                          }                
                      };
                          
                      var onLibraryUpdate = function () {
                          adobe.dps.store.getFolioData(onFolioData);
                          
                          // Unregister for library updates
                          unregisterUpdateLibraryHandler(); //commented out by Y.J.
                          
                      }
                  
                      var registerUpdateLibraryHandler = function ()
                      {
                        if (window.adobedpscontextloaded)
                        {
                          // call into the updateLibrary API
                          adobe.dps.store.registerLibraryUpdateCompleteHandler(onLibraryUpdate);
                          adobe.dps.store.updateLibrary(); //this needs to be here, otherwise, no button shows up 
                        }
                        else {
                            //console("Failed to find Javascript API");
                        }
                        
                      };
                      
                      var unregisterUpdateLibraryHandler = function ()
                      {
                        if (window.adobedpscontextloaded)
                        {
                          // call into the updateLibrary API
                          adobe.dps.store.unregisterLibraryUpdateCompleteHandler(onLibraryUpdate); 
                        }
                      }
                  
                      registerUpdateLibraryHandler();
                  
                      var buy = function(productID) {
                          var folio = folioDataHash[productID];
                          
                          if (folio==null) return;
                          if (folio.state=='100') {
                              adobe.dps.store.buyFolio(productID);
                          }else {
                              adobe.dps.store.viewFolio(productID);
                          }
                      };
                  
                      //showMsgBox brings up message box and black cover when user is not logged in
                      function showMsgBox() {
                  
                          var overlay = $('#black-cover');
                          var wrapper = $('#warning-mmsg');
                          overlay.css({'display':'block'});
                          wrapper.css({'display':'block'});
                  
                      }
                      
                      //showMsgBox brings up message box and black cover when user is not logged in
                      function showMsgBox1() {
                  
                          var overlay = $('#black-covers');
                          var wrapper = $('#warning-msgs');
                          overlay.css({'display':'block'});
                          wrapper.css({'display':'block'});
                  
                      }
                  
                       var consoleElement = document.getElementById("console");
                  
                  
                       var console = function(s) {
                           if (consoleElement) consoleElement.innerHTML += s + "<br/>";
                       }
                       
                      
                  } //END: init_dps_api function
                  
                  

                  This whole script works and in the storefront, all of the  5 buttons appear with the correct publication id in there buckets.

                   

                  But now I want to be able to pass another variable, in this case saleNumbers, where I would do a second push:

                   

                  <script>
                       publicationIDs.push('<%=publicationId%>'); 
                       saleNumbers.push('<//%=saleNumber%>'); // second push
                  </script>
                  

                  and pass that into the init_dps_api function like so:

                   

                  var init_dps_api = function(productIDs, saleNumbers) {
                  
                  }
                  

                  What I've done only printed the sale number for the first bucket, but I need it to print 5 different sale numbers for their respective buckets.

                   

                  Thanks for your time and patience.

                  • 6. Re: Pass in another variable thru onFolioData()
                    derek_lu Community Member

                    I'm not sure why you have to pass it to the callback. It sounds like it is a scoping issue since you want the variable accessible from within the callback. Did you try accessing publicationIDs or saleNumbers from the callback? It looks like they are in the global scope so they should be accessible. Are you declaring and creating the arrays before calling push()? Did you try defining a scope variable?

                    • 7. Re: Pass in another variable thru onFolioData()
                      BC_Sothebys Community Member

                      I have to pass the saleNumbers into the callback because in the onPreviewImage function the buttons are created here. I need to pass the salenumber and publicationid to an Omniture function from that button by using an onclick.

                       

                      The publicationIDs and saleNumbers are both global variables and as you say, they both should be accessible. However, the only variable that is accessible is the publicationIDs which doesn't make sense to me.

                       

                      These arrays are defined before calling the push().

                       

                      Ideally, this should all work, but I'm missing something.

                       

                      I am just understanding javascript, so I am not familiar with scope variables yet. Can you briefly explain?

                      • 8. Re: Pass in another variable thru onFolioData()
                        derek_lu Community Member

                        For a description of scope and closures I suggest reading any of the links from here https://www.google.com/search?q=javascript+scope+explanation.

                         

                        In this case your scope is changing when the callbacks are executed because they are being called from the scope of the caller not the scope from which the function is defined. To work around this I suggest using the following convention:

                         

                        var scope = this;

                        adobe.dps.store.getFolioData(function(data){ scope.onFolioData(data) });

                         

                        I suggest doing this for all DPS APIs that have callbacks.

                         

                        For updateLibrary() you could use:

                         

                        var scope = this;

                        adobe.dps.store.registerLibraryUpdateCompleteHandler(function(data){ scope.onLibraryUpdate(data) });

                         

                        Please give this a try and let me know how it goes.

                         

                        Once you have your scope issues sorted out you will have another problem with the following:

                        adobe.dps.store.getPreviewImage(productIDs[j], true, 768, 1024, onPreviewImage(productIDs[j]));

                        There are a few problems with this statement. The last parameter of getPreviewImage() takes a function. In your case, since you are executing onPreviewImage() you are passing in the return value of that function which in this case is void. What you want to do is pass the function. Another problem is that you can only have one callback at a time for getPreviewImage() so you must wait for one to complete before executing another one otherwise the first callback will get overwritten with the second. Have you tried getting the preview images from your fulfillment XML? If you have your accountId you can access your fulfillment url from http://edge.adobe-dcfs.com/ddp/issueServer/issues?accountId=. If you download and parse the XML you can get the preview image for your folio by getting the value in libraryPreviewUrl and appending with either /portrait or /landscape, ex: http://edge.adobe-dcfs.com/ddp/issueServer/issues/bdfb4254-1276-4393-abc6-3578540b750e/lib raryPreview/portrait.

                        • 9. Re: Pass in another variable thru onFolioData()
                          BC_Sothebys Community Member

                          Derek

                           

                          Thank you for your help. 

                           

                          I went back to

                          if (folio) {

                            adobe.dps.store.getPreviewImage(productIDs[j], true, 768, 1024, onPreviewImage(productIDs[j]));

                          }

                          in the code and changed the callback from

                          onPreviewImage(productIDs[j]) 

                          to

                          function(productID,saleNumber) {

                              onPreviewImage(productIDs[j],saleNumbers[j]);

                          }

                          Now it's working.

                           

                          Thanks again and have a great weekend.

                           

                           


                          • 10. Re: Pass in another variable thru onFolioData()
                            derek_lu Community Member

                            Just a word of caution, you might get the same value passed to onPreviewImage. I forget how the call works internally in the viewer but it might be an asynchronous call which will wait for the loop to execute before executing the callbacks which will cause "j" to be the last known value of "j" for all of the callbacks.

                            • 11. Re: Pass in another variable thru onFolioData()
                              BC_Sothebys Community Member

                              Derek

                               

                              I will definitely keep that in mind and next week I will also look at adding those scopes and see what happens.

                               

                              Again, have a great weekend.

                               

                              Sent from my iPhone