How do I get access to the api from outside the scope of init?

api

(robertt) #1

Having an airhead moment. I want to call api methods from the global space, not inside the client init scope. In the cameraUpdate function below, api is obviously undefined. How do I get a handle to it? Copying it somewhere global causes "object cannot be cloned" error.

var urlid = '7w7pAfrCfjovwykkEeRFLGw5SXS';

client.init( urlid, {
    success: function onSuccess( api ){
        api.start();
        api.addEventListener( 'viewerready', function() {
            //API is ready to use
            console.log( 'Viewer is ready' );
        } );
    },
    error: function onError() {
        console.log( 'Viewer error' );
    }
} );

function cameraUpdate(camera) {   
  api.lookat(
    camera.position,
    camera.target,
    1
  );                
}

Viewer api. multiple calls
#2

Just declare the api object globally and define it during client.init:

var urlid = '7w7pAfrCfjovwykkEeRFLGw5SXS';
var api;

client.init( urlid, {
    success: function onSuccess( callback ){
        api = callback;
        api.start();
        api.addEventListener( 'viewerready', function() {
            //API is ready to use
            console.log( 'Viewer is ready' );
        } );
    },
    error: function onError() {
        console.log( 'Viewer error' );
    }
} );

(robertt) #3

Hmmm.. Must be something else with my code. Above produces...

"Uncaught DataCloneError: Failed to execute 'postMessage' on 'Window': An object could not be cloned."

I'm essentially trying to use the approach here to remote operate a model. When a cameraUpdate() call comes in to the receiving browser I want to call api.lookat(). That seems to be working in the sharing experiment but any attempt I make to develop an accessible handle for the api meets with the cloning error above.

I'm thinking cross domain problem? Chrome reports "object could not be cloned" error but IE reports "Access denied"


#4

I don't know much about that specific example. Maybe @mauricesvay can help.

However, a global API object does work. See my fiddle here: http://jsfiddle.net/PadreZippo/kbkbzx31/


#6

I got a response notification but it seems you've deleted it. Did you figure it out?


(Rawinteractive) #7

Im having this exact problem, did you find the solution @rexdtripod? Thanks


#8

Do you have a code sample?

Just declare the api variable outside client.init, then define it as the callback of success - see my snippet above.