Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ioan
Roku Guru

Passing an array through interface field?

I have this array, a = [{id: 1, checked: false}, {id: 2, checked: true}] and I want to pass it to a task. I tried declaring the interface 
<field id = "checkedlist" type = "array" /> or <field id = "checkedlist" type = "assocarray"  /> 
but when I try to assign the a to it, it doesn't work. Here is what I tried:

m.save = createObject("roSGNode", "ConfigTask")
 m.save.checkedlist = a
 m.save.isarray = true
 m.save.isdone = false
 m.save.observeField("isdone", "closeThis")
 m.save.control = "RUN"


I also tried m.save.checkedlist.Append(a) and to push each element separately, but I always get error. What's the right way to do this?
https://github.com/e1ioan/
http://rokucam.com
0 Kudos
5 REPLIES 5
juantwc
Visitor

Re: Passing an array through interface field?

Hey

If you look here https://sdkdocs.roku.com/display/sdkdoc/interface you will see that the type of array you are trying to pass is not one of the supported types. The choice of supported types is a bit strange to be honest, but i digress.

In your case probably the best you can do is declare the field

<field id = "checkedlist" type = "assocarray"  /> 


And then wrap you array inside an assocarray, which should look something like this:

a = { 
checkedList :  [{id: 1, checked: false}, {id: 2, checked: true}]
}


And then pass it to your task. 
0 Kudos
EnTerr
Roku Guru

Re: Passing an array through interface field?

"juantwc" wrote:
If you look here https://sdkdocs.roku.com/display/sdkdoc/interface you will see that the type of array you are trying to pass is not one of the supported types. The choice of supported types is a bit strange to be honest, but i digress.

you are right re RSG bizarrely "reinventing" the data types of Brightscript -
but let's regress (dis-digress? pro-gress?): "array" is now one of the supported types (since 7.5)
You can have now array of dictionaries, dictionary of arrays or nest them any which way - just as in JSON.
0 Kudos
EnTerr
Roku Guru

Re: Passing an array through interface field?

"ioan" wrote:
... I also tried m.save.checkedlist.Append(a) and to push each element separately, but I always get error. What's the right way to do this?

That won't work between threads for sure, because `m.save.checkedlist` creates a local (deep) copy, to which appends `a` - and then the result will be dropped - the original `m.save.checkedlist` remaining unaffected.

By the way, note that `.append()` method name is a misnomer - if you come from e.g. Python background and think that adds 1 element to the end of the list, you'd be gravely mistaken - what it does instead is `list.extend(x)` in Python vernacular - i.e. it expects `x` to be an array of elements. Experiment in console first.

Python            Brightscript
------            ------------
list.append(elm)  roArray.push(elm)
list.extend(lst)  roArray.append(lst)
0 Kudos
EnTerr
Roku Guru

Re: Passing an array through interface field?

"ioan" wrote:
I have this array, a = [{id: 1, checked: false}, {id: 2, checked: true}] and I want to pass it to a task. I tried declaring the interface 
<field id = "checkedlist" type = "array" /> or <field id = "checkedlist" type = "assocarray"  /> 
but when I try to assign the a to it, it doesn't work. Here is what I tried:
m.save = createObject("roSGNode", "ConfigTask")
 m.save.checkedlist = a
...


For troubleshooting purposes, let's try doing this in the simplest way possible - do not even declare it - just do

m.save.addFields({checkedlist: [{id: 1, checked: false}, {id: 2, checked: true}]})
? formatJSON(m.save.checkedlist)

Does it work?
0 Kudos
ioan
Roku Guru

Re: Passing an array through interface field?

"EnTerr" wrote:

Does it work?


I'll test it tomorrow and let you know. BTW, I released a beta for the IP Camera Viewer channel.
https://github.com/e1ioan/
http://rokucam.com
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.