7 Replies Latest reply on May 23, 2006 1:40 PM by ntsiii

    Global Variables and scope

    captn steve
      Ok, I'm creating a fairly large RIA using many components set on a page. They all share a common list. It is a Project list that is displayed in a DataGrid. So far, no problem. I then have a simple detail component in a panel that when an item is clicked in the grid, the item is to be passed to the detail component and that item is used as the detail components dataProvider.

      I have an actionscript file that is used by both the grid component and the detail component. I think that is were the problem lies.

      Here is how I would like it to work:
      On the Grid Component, when the selected item is changed, that item is passed to a variable (var selectedProject) that is declared in the ActionScript file. Here is the declaration code for the Grid component:

      <?xml version="1.0" encoding="utf-8"?>
      <mx ataGrid xmlns:mx=" http://www.adobe.com/2006/mxml" height="100%" width="100%" dataProvider="{dataObject}" change="selectedProject=this.selectedItem">
      <mx:Script source="ActionScripts/Start_Scripts.as">

      Note that selectedProject is in the Actionscript file 'Start_Scripts.as' I have also verified using an alert that the object is in fact stored in that variable and I can access the individual fields of the object.

      Now within my detail component is a textbox. here is the declaration code for that:

      <mx:FormItem label="Project Name" horizontalAlign="right" required="true">
      <mx:TextInput maxChars="40" toolTip="Project Name" id="txtProjectName" width="125" text="{selectedProject.ProjectName}"/>

      Needless to say it doesn't work when I click on the items in the grid :-(.

      I know it is a lot of info, but any assistance would be appreciated.
        • 1. Re: Global Variables and scope
          ntsiii Level 3
          How is your detail component implemented? Is it a custom mxml or as component?

          Also how are you including the 'Start_Scripts.as' file? You said this was used in bothe the main application and in the component?

          If you are using include or mx:Script sourec=.. then you are getting a separate copy of the file in each component, they have no connection with each other. Is this what you want/expect?

          If you want to pass "selectedProject" into the detail component, declare a public variable in the component an pass the item object in via that:
          in the component:
          public var selectedProject bject;
          and then bind the control to it as you are doing

          and when you declare the component:
          <myDetailComponent ... selectedProject="{selectedProject}" .../>

          This way, any changes to the selectedProject var in the main app will be transmitted into the component, an the detail control will update.,

          • 2. Re: Global Variables and scope
            captn steve Level 1

            Typically that is how I do it. I created a variable called dataProject in the component and passed the selected item of the DataGrid into that. Tha worked fine. However when passing in a Variable that was declared in the .as file it didn't work. Maybe I'm just not doing something right in the declaration of the component. I'll take a look at your implementation, and if it doesn't work for me, I'll let you know!


            • 3. Re: Global Variables and scope
              ntsiii Level 3
              Also how are you including the 'Start_Scripts.as' file? You said this was used in both the main application and in the component? Are you sure you want a copy of that code in both the main app and in the component?
              • 4. Re: Global Variables and scope
                captn steve Level 1
                " have an actionscript file that is used by both the grid component and the detail component. "

                It is referenced by both components. They use similar code so it makes sense to me to encapsulate all of that code into a code file that can be referenced by whatever components need it. Of course, that might not be best practices for Flex. I don't know. There are many plusses I have found using Flex over ASP.net for this app, and I am trying to incorporate concepts that make sense to me. Reusable code is a big one. Session objects would be another. That would most likely solve my issue here, however, I can't be sure how databinding will react to it. Trial and error at this point, however I'm open to suggestions

                • 5. Re: Global Variables and scope
                  ntsiii Level 3
                  Sure, if both files really need the same scripts. Note that depending on how you are probably including the file, it is not "referenced", rather it is a full copy. I just would not expect the main app and a component to have the same script.

                  I do not advise using sessions unless your app truly requires it. One of the primary benefits of Flex is that it allows you to maintain state on the client. Only if your app becomes so complex, where you need to navigate between different urls, should you need to use sesions. It would not help this issue.

                  • 6. Re: Global Variables and scope
                    captn steve Level 1

                    Like I said, I'm still trying to wrap my head around the concepts this being more like an app than a website in some instances. Let me ask you this then. If I include the script source in the main page do the custom components have visibility to that code? If so then I guess that is the ideal way for me to work with the code. I currently don't have the script file included on my main page. I have it included in the individual components that are on the page. Idealy I would have a seperate script file for each mxml file.

                    • 7. Re: Global Variables and scope
                      ntsiii Level 3
                      No, the scope of the script is only within the component (the main app is really a "component" too) You CAN reference members back up the document structure, using parentDocument(one level higher) and mx.core.Application.application(which is always the top level, main app scope)

                      What you are doing is fine, now that I understand. Having a standard set of script code for all components is perfectly reasonable. I had misunderstood that you had the same code in the main app. You can include multiple .as files as well, so you can have a standard set as well as component specific code.

                      You can pass references to anything to a component. This means you can pass a reference to the detail component into the grid component, or vice-versa, if you need.