Marvin JS sketcher execution

User eda6f877b4

11-09-2014 10:46:22

Hi 


I am working on a solution like the example edit-images.


Most of it is working OK, but I am having trouble getting a reference to my sketcher instance. I am using Telerik control (RadWindow) to open the iframe. The iframe is created once the image is clicked and not on page load.


Code block 'oWnd.show();' initiates the creation of the iframe.


I have the following JS function that is fired when a image is clicked. This opens the sketcher in an iframe:


                                            function clickOnImage(pict) {


                                                currentPict = pict;


                                                var oWnd = $find("modalWindowEditStructure");


                                                oWnd.show();


                                                oWnd.get_contentFrame().id = "sketchImg";


------------------- EXECUTED LAST -------------------------


                                                MarvinJSUtil.getEditor("#sketchImg").then(function (sketcherInstance) {


                                                    marvinSketcherInstance = sketcherInstance;


                                                }, function () {


                                                    alert("Cannot retrieve sketcher instance..!");


                                                });


------------------- EXECUTED LAST -------------------------


                                                if (lastmol && marvinSketcherInstance) {


                                                    //Import mol


                                                    marvinSketcherInstance.importStructure("mol", lastMol).catch(function (error) {


                                                        alert(error + ".. that was the error!");


                                                    });


                                                }


                                                else {


                                                    alert("Sketcher not available..!");


                                                }


                                            }


 


The problem here is as follows:


The code marked above and starting with:  'MarvinJSUtil.getEditor' is excecuted AFTER the code below where I need to use the sketcher instance!


Because of that the sketcher is not available when needed. 

ChemAxon 7c2d26e5cf

12-09-2014 11:07:20

I would modify your code like this:


function importStructure(sketcher) {
  if(lastMol) {
    //Import mol
    sketcher.importStructure("mol", lastMol).catch(function (error) {
      alert(error + ".. that was the error!");
    });
  }
}

function clickOnImage(pict) {
  currentPict = pict;
  var oWnd = $find("modalWindowEditStructure");
  oWnd.show();
  oWnd.get_contentFrame().id = "sketchImg";
  if(marvinSketcherInstance) {
    importStructure(marvinSketcherInstance);
  } else {
     MarvinJSUtil.getEditor("#sketchImg").then(function (sketcherInstance) {
       marvinSketcherInstance = sketcherInstance;
       importStructure(sketcherInstance);
     }, function () {
      alert("Cannot retrieve sketcher instance..!");
     });
  }
}


If marvinSketcherIntance has not been defined when you want to setup the molecule for the editor, the callback of getEditor promise can complete it.

User eda6f877b4

12-09-2014 11:11:55

OK, I will try and let you know. Thanks!

User eda6f877b4

12-09-2014 12:16:01

Thanks for this.. it is now working as exptected  .