belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2017
03:57 PM
Re: Accessing a global object inside a SceneGraph Component
That's the idea. Try it and see what happens.
Motorcykey
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2017
05:11 PM
Re: Accessing a global object inside a SceneGraph Component
I just had the chance to try and my observer is not working. Or at least what I'm finding is that it's not being dispatched because 'm.global.usertype' isn't actually changing.
The code looks to be getting executed, however, when I output the 'm.global' SG Node afterward, I receive the following:
The 'usertype' property is there, but still contains the value it was initialized at, which tells me there is something wrong with how I'm attempting to set the value in my else block.
I can comment up my code to hopefully give you a better idea of what I'm trying to achieve:
The code looks to be getting executed, however, when I output the 'm.global' SG Node afterward, I receive the following:
<Component: roSGNode> =
{
change: <Component: roAssociativeArray>
focusable: false
focusedChild: <Component: roInvalid>
id: "GlobalNode"
usertype: "Preview"
}
The 'usertype' property is there, but still contains the value it was initialized at, which tells me there is something wrong with how I'm attempting to set the value in my else block.
I can comment up my code to hopefully give you a better idea of what I'm trying to achieve:
SubscribeScreen.brs
if (m.global = invalid)
' This block executes when the user opens the screen for the first time
m.global = screen.getGlobalNode()
m.global.id = "GlobalNode"
m.global.addFields( {userType: "Preview"} ) ' This line adds "userType": "Preview" to the m.global roSGNode
else
' on subsequent opens, 'm.global' has been initialized, so we just want to update its 'userType' value which could have changed
m.global.userType = "Monthly"
end if
RowListScene.brs
function init()
m.top.backgroundURI = "pkg:/images/background.png"
m.top.backExitsScene = true
m.global.observeField("userType", "userTypeChanged") ' setting up my observer in the init() function
...
end function
' defining my observer handler
function userTypeChanged()
' but I never see this called, most likely because I have never seen the value update
print "USER TYPE HAS CHANGED"
m.userType = m.global.userType
end function
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2017
05:29 PM
Re: Accessing a global object inside a SceneGraph Component
Have you confirmed that the code m.global.userType = "Monthly" is actually executing?
Motorcykey
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2017
05:36 PM
Re: Accessing a global object inside a SceneGraph Component
Only in that a print statement will log out when placed in that else block. Also, the app doesn't get hung up. It's an assumption, but I'm unsure how I would 100% confirm.
Motorcykey
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2017
05:48 PM
Re: Accessing a global object inside a SceneGraph Component
If I place print statements before & after the line:
m.global.userType = "Monthly"
Both print statements log out in my debugger. So I am inclined to think that the code is being executed, but again it is just an assumption.
m.global.userType = "Monthly"
Both print statements log out in my debugger. So I am inclined to think that the code is being executed, but again it is just an assumption.
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2017
06:23 PM
Re: Accessing a global object inside a SceneGraph Component
You could put a print statement in SubscribeScreen.brs to see if it is getting initialized again.
Motorcykey
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2017
09:24 AM
Re: Accessing a global object inside a SceneGraph Component
I found my solution and I think I need to abandon any hope of using the Global Node. I went with adding a new field to my screens interface:
<field id="userType" type="string" />
I was able to modify this value by setting it right before the while loop (and after the screen.Show() method) of my SubscribeScreen.brs file:
scene.userType = m.context.subscriptionType
Which I was then able to add an observer if any changes were made to "m.top.userType" within RowListScene.brs:
m.top.observeField("usertype", "userTypeChanged")
<field id="userType" type="string" />
I was able to modify this value by setting it right before the while loop (and after the screen.Show() method) of my SubscribeScreen.brs file:
scene.userType = m.context.subscriptionType
Which I was then able to add an observer if any changes were made to "m.top.userType" within RowListScene.brs:
m.top.observeField("usertype", "userTypeChanged")
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017
07:08 PM
Re: Accessing a global object inside a SceneGraph Component
"Motorcykey" wrote:
I didn't! So it sounds like it's best practice that for any SceneGraph Component I create that needs access to my functions from util.brs, I would basically "require" them as a dependency as follows. Makes sense!<script type="text/brightscript" uri="pkg:/components/util.brs" />
It is not a "best practice" - it is "the only practice", since there is no other way to do it. Each XML component you define sees only the scripts explicitly pointed at, each lives in its own little world.
And for amusement's sake, i can say that this is also a "worst practice", just like it is a "best practice" - when you have freedom of choice of only 1 item, it's both the best and the worst in the series :mrgreen:
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017
07:26 PM
Re: Accessing a global object inside a SceneGraph Component
"Veeta" wrote:"joetesta" wrote:
... Within the same file, define the functions '_someFunction' and '_getSomething'
Now you can instantiate myUtils from anywhere ( util = myUtils() ) and pass in variables to the defined methods ( result = util.getSomething(somevars) ).
Your example hit upon nearly the same pattern i use for my brs modules. I don't think it's been formalized or blogged about anywhere but it would be nice if more Roku developers adopted this style. I'll leave in-depth discussion for a different thread.
There is very little to no utility in doing so if there is no real data in the object.
Why do it then? You cannot pass the resulting AA outside scope (because: RSG) and inside the scope the "underscore" functions are already visible, so...
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2017
05:47 PM
Re: Accessing a global object inside a SceneGraph Component
"Motorcykey" wrote:
Setting the values works when the screen is first called, however, the screen can be opened more than once. For example, logging out and logging back in again.
Current attempts to log back in just freeze the app when hitting the line:m.global.addFields
I can't seem to call any roSGNode methods on m.global from SubscribeScreen.brs, however I can inside RowListScene.brs.
Wait a minute - are you telling me that you create >1 roSgScreen - as in, multiple times creating and closing a roSgScreen? And that you expect .getGlobalNode() on different RSG screens to return the same value (and not say, alternatively hang or crash because nobody has tried doing such crazy thing 🙂 )?
- « Previous
-
- 1
- 2
- Next »