Your Digital Media Has Never Looked So Good

 
ioan
Topic Author
Posts: 279
Joined: Mon Jan 10, 2011 2:02 pm

Passing an array through interface field?

Sat Feb 18, 2017 3:21 pm

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
 
juantwc
Posts: 15
Joined: Thu Nov 26, 2015 3:23 am

Re: Passing an array through interface field?

Sun Feb 19, 2017 1:56 am

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. 
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Passing an array through interface field?

Sun Feb 19, 2017 5:08 pm

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.
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Passing an array through interface field?

Sun Feb 19, 2017 5:25 pm

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)
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Passing an array through interface field?

Sun Feb 19, 2017 5:52 pm

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?
 
ioan
Topic Author
Posts: 279
Joined: Mon Jan 10, 2011 2:02 pm

Re: Passing an array through interface field?

Sun Feb 19, 2017 10:03 pm

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

Who is online

Users browsing this forum: No registered users and 6 guests