Search code examples
apache-flexfunctionviewmxml

How do I access a public function outside of the view it is in using Flex?


Hi, I have been working on a Flex Mobile application using Flash Builder 4.6.

I have 2 mxml 'views' in my project. In one mxml file, i have a function that grabs xml data. In my other mxml file, I have a refresh button that when depressed is suppsosed to call the function in the first mxml file in order to once again grab the xml data. I dont know how to call that function from outside the mxml file it is housed in.

I appreciate any help given. Thank you!

[UPDATE #2]*

I thought I should share some more details about my issue.

  1. It is a reddit client mobile app. It fetches the feeds, etc.
  2. In my main view called RedditReaderHomeView.mxml, I am using a splitViewNavigator spark component to house two other views like so:

RedditReaderHomeView.mxml

    <s:SplitViewNavigator width="100%" height="100%" id="splitViewNavigator" autoHideFirstViewNavigator="true">
        <s:ViewNavigator id="redditList" firstView="views.subredditList" width="300" height="100%"/>
        <s:ViewNavigator id="redditFeed" firstView="views.redditFeed" width="100%" height="100%">

            <s:actionContent.landscape>
                <s:Button id="refreshButtonlLandscape" icon="@Embed('assets/refresh160.png')" click="refreshRSS()" />
            </s:actionContent.landscape>

            <s:actionContent.portrait>
                <s:Button id="refreshButton" icon="@Embed('assets/refresh160.png')"  />
                <s:Button id="navigatorButton" label="Search" click="splitViewNavigator.showFirstViewNavigatorInPopUp(navigatorButton)" />
            </s:actionContent.portrait>
        </s:ViewNavigator>
    </s:SplitViewNavigator>
  1. As you can see in the code above, in my main view I have a button with the id "refreshButton." When I click this button, I want the reddit data to refresh. In other words I want to call a function to refresh the data, that is housed in the view, 'redditFeed'.
  2. This is the function which is in a separate view named 'redditFeed.mxml', that I want to call using the refresh button in the main view shown above.

redditFeed.mxml

    protected function myList_creationCompleteHandler(url:String):void
            {

                getRedditFeedResult.token = redditFeedGrabber.getRedditFeed(url);
                getRedditFeedResult.addEventListener(ResultEvent.RESULT,busyOff);

            }

I hope this helped clear out confusion as to what I was trying to do. Im assuming that the solution is quite simple, but alas, I am a novice programmer and new to Flex, so Im learning the ropes. Any help is appreciated. Thank you!


Solution

  • IF you have an instance of the view, then just do:

    myViewInstance.myPublicFunction();  
    

    In MXML, the id element of the MXML tag is used to reference the view in ActionScript. Since you didnt' describe your architecture; it is unclear how one view can call the other.

    If the view that needs to trigger the call is a parent of the view that has the function to make the call, then you could use the approach described above.

    If the view that need to trigger the call is a child of the view that has the function to make the call, then you should dispatch an event from the "child" which the parent can listen to. In the event handler you would trigger the call.

    If the view that needs to trigger and the view that has the function to make the call are both children of the same parent; then you should dispatch an event from the "Trigger" view, listen for it in the parent, and then use that event listener to make the call (Using similar code to what I explained above).

    If you have a more complicated architecture of these two views; then you should look into some method to encapsulate the "remote call" functionality, such as into a service class. Many frameworks offer approaches to share that service class and/or results across multiple classes. ( MXML Files are classes).