Getting result from calc into javascript code

  • Hi

    I have a javascript function to filter scenes. The filter creteria expression sits in a attribute of another xml element, e.g. like this:

    Code
      <lkmenu name="expomenu" title="Exposition" linkedscenefilter="get(group)=='expo'" />

    My code filters the scenes by constructiong some action code from the linkedscenefilter expression. That code assigns the result to a variable (in my plugin) and as the next action I fetch the variable value with get:

    This implementation mostly works, but there are 2 problems:

    • It is very, very ugly, since calculation and retrieving the result are separated and a intermediate variable is used! *cry*
    • Sometimes, mainly on the first call while krpano is still busy, the code fails since "call" is async resp. puts the commands in a queue and "get" is sync. Therefore "get" is sometimes executed before the check code has been executed and therefore returns a wring result.

    Is there a way to make the code a bit nicer ( return the result of the calc directly to the javascriopt code instead of using calc first and then fetching the result with get) and even more important to ensure that the check expression is always executed before the result is fetched (or again to return the result directly)?

    I'd be grateful for some advice how to work around this. Thanks!

  • Hi,

    here your code a bit modded to work without call:


    The calc action can be also called directly from JS and will return its result (the variable where the result should be stored can be set to null for this case).

    Although this code is still not very nice, because it needs to manually change and restore the actioncaller to use the scene object as local variable scope for your expression...

    For the next release I will therefore add a direct JS-API for resolving an expression and optionally also setting the caller the same time.


    Btw - depending on your needs, you could do also a direct check in Javascript code instead of using an expression, e.g. do something like that:

    Code
    for (var i = 0; i < krpano.scene.count; i++) {
    	const scene = krpano.scene.getItem(i);
    	if(scene.group == 'expo') {
    		//further processing if the result is true
    	}
    }

    or like that:

    Code
    krpano.scene.getArray().forEach( function(scene) {
    	if(scene.group == 'expo') {
    		// ...
    	}
    });


    Best regards,
    Klaus

  • Thanks for the input! *thumbsup*

    The approach by Klaus is great! It does exactly what I need. A direct api call as mentioned by Klaus would be even more helpful, but for the time been the proposed solution is more than good enough


    The other solutions like

    Code
    krpano.scene.getArray().forEach( function(scene) {
    	if(scene.group == 'expo') {
    		// ...
    	}
    });

    would be great if there is a fixed check expression, but they dont work for me, because I want to to be able to specifiy the check expression in action code style in a attribute.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!