You are not logged in.

aperture147

Beginner

  • "aperture147" started this thread

Posts: 17

Location: Vietnam

Occupation: DevOps

  • Send private message

1

Friday, March 12th 2021, 5:14pm

Value-Array cannot be pushed

I'm using Value Array to store simple data. The problem is that the array could not be access in Krpano code, but cpuld be change in js (in both console and JS action). Mixing between those doesn't look neat at all. Here are some example:

Source code

1
2
3
4
5
6
<action name="init_action" autorun="preinit">
	def(some_array, array);
</action>
<action name="sample_action" scope="local">
	some_array.push('test');
</action>

When I call the action "sample_action", the console popped out this error:

Source code

1
Uncaught TypeError: "length" is read-only

Is this a bug or I'm doing something wrong?
Thanks
KRPano Showcases:
Showcase 1
Showcase 2

2

Friday, March 12th 2021, 7:36pm

a krpano value array is not a js array... it has no push function.
https://krpano.com/docu/plugininterface/#array
https://krpano.com/docu/actions/#arrays

that wouldn't be a bad addition, though .)

3

Saturday, March 13th 2021, 10:03am

Hi,

the 'value arrays' in krpano ARE just Javascript Arrays, but the way krpano calls Javascript functions doesn't work for the Array push function (that's a Javascript special case).

When doing this in krpano action code:

Source code

1
some_array.push('test'); 

the resulting JS code will be:

Source code

1
(some_array.push).apply(some_array, 'test')

and that 'apply' doesn't work for the Array push/pop functions (because of the way how they internally work).

As alternative you could either extend krpano with some custom push/pop functions for value arrays or directly use Javascript Actions.

Best regards,
Klaus

aperture147

Beginner

  • "aperture147" started this thread

Posts: 17

Location: Vietnam

Occupation: DevOps

  • Send private message

4

Saturday, March 13th 2021, 1:26pm

Thanks for the explain! *thumbsup* I think I just need to make a work-around code like this:



Source code

1
2
3
4
5
6
7
8
9
10
<!--Push data to value array
args:
args[1]: name of the array
args[2]: value
-->
<action name="value_array_push" type="Javascript">
<![CDATA[
krpano.get(args[1]).push(args[2])
]]>
</action>

or call the push directly from jscall().
Btw it would be nice to add some special function which could not be called to the official document.
KRPano Showcases:
Showcase 1
Showcase 2

5

Saturday, March 20th 2021, 11:14am

Hi,

nice workaround ;-)

Here the same slightly optimized (predefined as Javascript function):

Source code

1
2
3
4
5
6
<action autorun="preinit" type="Javascript">
  actions.value_array_push = function(array, val)
  {
    resolve(array).push(val);
  }
</action>


Or here another possibility - a custom function that 'extends' an Array object with a push function that works:

Source code

1
2
3
4
5
6
7
8
<action autorun="preinit" type="Javascript">
  actions.add_push_support = function(array)
  {
    array = resolve(array);
    array._push = array.push;
    array.push = function(v){ array._push(v); };
  }
</action>


To use it:

Source code

1
2
3
4
5
6
7
8
def(some_array, array);
add_push_support (some_array);
some_array.push('test');
some_array.push('test2');
...
set(debugmode,true);
debugvar(test);
showlog();



Quoted

Btw it would be nice to add some special function which could not be called to the official document.

Do you mean Javascript functions?
There are no general limitations, but the Array.push function is a special case in Javascript, please search for 'array push apply' to find more detailed explanations about that.

Best regards,
Klaus