User 646de8ab9c
21-01-2015 16:28:21
Hi,
I have multiple marvinSketcherInstances in a single html page. And I want to export the structures of all the instances through an hidden element of a form.
First I tried this:
function initControl () {
$("#btn-valid").on("click", function (e) {
marvinSketcherInstance[0].exportStructure("mol").then(function(source) {
document.getElementById('entree1').value=source;
}, function(error) {
alert("Molecule export failed:"+error);
});
marvinSketcherInstance[1].exportStructure("mol").then(function(source) {
document.getElementById('entree2').value=source;
}, function(error) {
alert("Molecule export failed:"+error);
});
submitform();
});
}
This is not working. Only one element is completed.
Finally, after one day of working on, I find this solution: add setTimeout around the submitform function to wait that all the element are completed.
function initControl () {
$("#btn-valid").on("click", function (e) {
marvinSketcherInstance[0].exportStructure("mol").then(function(source) {
document.getElementById('entree1').value=source;
}, function(error) {
alert("Molecule export failed:"+error);
});
marvinSketcherInstance[1].exportStructure("mol").then(function(source) {
document.getElementById('entree2').value=source;
}, function(error) {
alert("Molecule export failed:"+error);
});
setTimeout(function(){
submitform();
}, 500);
});
}
And now and tried to work with loops in order to work with more than 2 instances. I can initialize the instances whitout any problem, BUT I tried to used a "for" loop to export the structures and complete the elements whitout success:
function initControl () {
$("#btn-valid").on("click", function (e) {
for (i = 0; i < currCible; i++) {
var j = i + 1;
var entree = "entree"+j;
marvinSketcherInstance.exportStructure("mol").then(function(source) {
document.getElementById(entree).value=source;
}, function(error) {
alert("Molecule export failed:"+error);
});
}
setTimeout(function(){
submitform();
}, 500);
});
}
Here only the second element is completed.
A solution?
And why do I need to put "wait" function ?
Lionel