"coldrain" wrote:
I think you should use the task like this:
Init the showTask:
m.showTask = CreateObject("roSGNode", "SimpleTask")
m.showTask.ObserveField("showContentAA", "receiveContentFromServer")
When you select an item from the grid, assign the showIndex value (don't observe this field) and call m.showTask.control = "RUN", then the task will be triggered and call your getContent function. Then the getContent function will retrieve the data from the server and assign to showContentAA which will fire the function receiveContentFromServer.
Btw, I don't load everything upfront but only category and don't see any performance hit.
Thank you so much. I was observing the wrong field. I am caching things and show loading indicator if we have to hit the server. I had a working version of this a few years ago then things became deprecated. That is why I have terminology like "showLeaves."
ShowTask.xml<?xml version="1.0" encoding="UTF-8"?>
<component name="ShowTask" extends="Task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://devtools.web.roku.com/schema/RokuSceneGraph.xsd">
<interface>
<field id="showID" type="string" alwaysNotify="true"/>
<field id="showContentArray" type="array" alwaysNotify="true"/>
</interface>
<script type="text/brightscript" uri="pkg:/source/utils.brs" />
<script type="text/brightscript" uri="pkg:/components/Tasks/ShowTask.brs" />
</component>
ShowTask.brssub init()
m.top.ShowID = m.top.findNode("ShowID")
m.top.showContentArray = m.top.findNode("showContentArray")
m.top.functionName = "executeTask"
end sub
function executeTask() as void
if m.top.ShowID <> ""
m.top.showContentArray = getShowLeaves(m.top.showID.ToStr())
end if
end function
Set up task in init() of HomeScene.brs m.showTask = CreateObject("roSGNode", "ShowTask")
m.showTask.ObserveField("showContentArray", "setContentFromServer")
OnRowItemSelectedFunction OnRowItemSelected()
m.gridScreen.visible = "false"
selectedItem = m.GridScreen.focusedContent
if m.seasonsEpisodesAA.DoesExist(selectedItem.id) then
m.tlnShowScreen.showGridContent = m.seasonsEpisodesAA[selectedItem.id]
ShowScreen(m.tlnShowScreen)
else
m.loadingIndicator.text = "Loading " + selectedItem.title + "..."
m.loadingIndicator.control = "start"
m.showTask.ShowID = selectedItem.id
m.showTask.control = "RUN"
end if
End Function
SetContentFromServerFunction setContentFromServer()
selectedItem = m.GridScreen.focusedContent
ShowRowItems = createObject("RoSGNode","ContentNode")
for each itemAA in m.showTask.showContentArray
row = createObject("RoSGNode","ContentNode")
row.Title = itemAA.Title
for each episodeAA in itemAA.Episodes
item = createObject("RoSGNode","ContentNode")
item.SetFields(episodeAA)
row.appendChild(item)
end for
ShowRowItems.appendChild(row)
end for
m.seasonsEpisodesAA.AddReplace(selectedItem.id,ShowRowItems)
m.seasonsEpisodesAA.AddReplace(selectedItem.id, ShowRowItems)
m.tlnShowScreen.showGridContent = m.seasonsEpisodesAA[selectedItem.id]
m.loadingIndicator.control = "stop"
ShowScreen(m.tlnShowScreen)
End Function