Forum Discussion

ioan's avatar
ioan
Roku Guru
9 years ago

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?

5 Replies

  • 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. 
  • "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.
  • "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)
  • "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?
  • "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.