### Web Services - Sample 12: Multi-Origin with JavaScript API

quit pymol (FireFox only: view page source)

PyMOL should open up automatically when this page is loaded because we have an hidden IFRAME referencing a PWG file which tells PyMOL to launch and listen on port 8082.

In the multi-origin scenario, the host and port of the PyMOL server must be specified when creating the PyMOL object inside JavaScript:

    var pymol = new PyMOL('localhost',8082); // create PyMOL object instance for server of this page

    var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API

The other main difference is that in the multi-origin (cross-domain) scenario, all PyMOL requests run asynchronously and in an undefined order (in certain browsers). Any results to be acted on must be procesed through a call-back mechanism, and any dependencies of PyMOL state which exist must be broken out across separate function calls. See sample 14 for a practical to this problem of asynchronous execution.

The convention we have adopted for doing this is quite simple: you merely provide the callback function as the first argument to the method. The Javascript API automatically recognizes this pattern and removes that argument before sending the remaining arguments over to PyMOL.

    function part4(list) { for(var i=0;i<list.length;i++) {
        cmd.color("auto","chain "+list[i]); } }

    function part3(list) {
        cmd.show_as("cartoon");
        cmd.get_chains(part4, "polymer");
    }

    function part2() {
        cmd.load(part3, "$PYMOL_PATH/test/dat/1tii.pdb");
    }

    function part1() {
        cmd.reinitialize(part2);
    }

    Start the cascade with a call to part1()

For a solution to the awkward complexity presented by the above example, please see Sample 14.

Also, exceptions which occur in PyMOL are not presently returned to the JavaScript layer (though this may change).