8 Replies Latest reply on Jul 5, 2009 2:23 AM by rbz.

    Remote mx:Model source not refreshing

    Kaboom42 Level 1

      Hi,

       

      I'm working on a little project, which involves an <mx:List> object being populated by an <mx:Model>. This Model takes a remote XML file as a source.

      However, when I update the XML file, the only way to get the List object to take the new data is to recompile the application. Obviously, I don't want to have to recompile for every update to the XML file, as there will be quite a lot in future.

       

      Does anyone know how I can get Flex to not cache the XML file?

       

      Thanks.

        • 1. Re: Remote mx:Model source not refreshing
          leybniz Level 4

          Sure, you 'd better load your xml file at runtime using HTTPService class, you can find example here

          http://forums.adobe.com/thread/457489?tstart=0

          • 2. Re: Remote mx:Model source not refreshing
            Kaboom42 Level 1

            Hmm. Thanks for the quick response.

            However I'm new to Flex, so I'm not too sure about how to fix it!

            Maybe if I post the code someone could give me a pointer to get it running? Would be much appreciated.


            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
            <![CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            import mx.controls.Alert;

            private function selectChapter():void {
            var sendData:Object = {story:selectedStory.data, chapter:selectedChapter.data};
            chapterContent.htmlText = "Loading...";
            chapterSelect.send( sendData);
            }

            private function handlePlain(event:ResultEvent):void {
            var chapterText:String = event.result.toString();
            chapterContent.htmlText = chapterText;
            }


            private function handleFault(event:FaultEvent):void {
            Alert.show(event.fault.faultString, "Error");
            }

            [Bindable]
            public var selectedStory:Object;
            public var selectedChapter:Object;
            ]]>
            </mx:Script>
            <mx:Model id="storyList" source="http://www.adamhaskell.net/stories/storyList.php" />
            <mx:HTTPService id="chapterSelect" url="http://www.adamhaskell.net/stories/getStory.php"
            result="handlePlain(event);" fault="handleFault(event);" resultFormat="text" />
            <mx:Label text="Kaboom's Stories" fontSize="24" textAlign="center" width="100%"/>
            <mx:HBox width="100%">
            <mx:VBox height="100%" width="250">
            <mx:List id="stories" dataProvider="{storyList.story}" change="this.selectedStory=List(event.target).selectedItem;" width="100%"></mx:List>
            <mx:List id="chapters" dataProvider="{selectedStory.chapter}" change="this.selectedChapter=List(event.target).selectedItem;selectChapter();" width="100%"></mx:List>
            </mx:VBox>
            <mx:Text width="100%" height="100%" id="chapterContent"/>
            </mx:HBox>
            </mx:Application>

             

             

            Thanks again.

            • 3. Re: Remote mx:Model source not refreshing
              rbz.

              I was stuck on the EXACT same thing, but i modifed my code to use the HTTPService to read the XML into an ArrayCollection like so;

               

              public function init():void
                          {
                             
                              username = "rob";
                              xmlService.url = 'http://www.example.co.uk/users/' + username + '/snorklets.xml?rnd=' + new Date().getMilliseconds().toString();
                                 xmlService.send();

               

              }
                         
                 
                          public function resultHandler(event:ResultEvent):void
                          {
                             
                                  for each (var t:XML in new XML(event.result)..snork.snorklet.@name)
                                      ac.addItem(t);
                                   out.dataProvider = ac;
                                                      
                          }

               

              This code readss the XML from the site and then reads it into an ArrayCollection

               

              just setup a binding variable, and in the list data provider put {ac},

               

               

              i couldn't help you if you wanted to stay with models sorry.

              1 person found this helpful
              • 4. Re: Remote mx:Model source not refreshing
                Muzak Level 3

                mx:Model is not runtime, but compiled into your swf. That's why you have to recompile the project when making changes to your source.

                http://livedocs.adobe.com/flex/3/html/help.html?content=datamodels_4.html

                 

                As mentioned, use an HTTPService to load data at runtime.

                http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html

                • 5. Re: Remote mx:Model source not refreshing
                  Kaboom42 Level 1

                  Hmm... thanks for the tips, but I still can't get it to work...

                   

                  My XML file has this strucutre:

                   

                  <stories>

                       <story label="Title 1" data="key1">

                            <chapter label="Chapter 1" data="1" />

                            <chapter label="Chapter 2" data="2" />

                            ...

                       </story>

                       ...

                  </stories>

                   

                  What I'm trying to do is have two List objects. The first one is populated with the list of stories when the app is started. When a story is selected, I want the second List to be populated with the list of Chapters. (When a chapter is selected, its contents are loaded into the Text object - that much works!)

                  • 6. Re: Remote mx:Model source not refreshing
                    rbz. Level 1

                    omething i threw together:

                     

                    you'll have to replace the URL with the location of your XML.

                     

                    if this doesn't work or you need more help please let me know, im still a flex newb myself.

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="absolute">
                         <mx:Script>
                              <![CDATA[
                                   import mx.collections.ArrayCollection;
                                   import mx.rpc.events.ResultEvent;
                                   
                                   [Bindable]
                                   private var ac:ArrayCollection = new ArrayCollection();
                                   [Bindable]
                                   private var ac2:ArrayCollection = new ArrayCollection();
                                   [Bindable]
                                   private var ss:int = 0;
                                   
                                   private function init():void
                                   {
                                        getStory.send();
                                   }
                                   
                                   private function resultHandler(event:ResultEvent):void
                                   {
                                        ac.removeAll();
                                        ac2.removeAll();
                                        
                                        for each (var t:XML in XML(event.result)..story.@label)
                                             ac.addItem(t);
                                                                  
                                  for each (var t2:XML in XML(event.result)..story[ss].chapter.@label)
                                             ac2.addItem(t2);
                       }
                              ]]>
                         </mx:Script>
                         
                         <mx:HTTPService id="getStory" resultFormat="e4x" url="http://www.snorksoft.co.uk/storys.xml" result="resultHandler(event)"/>
                         
                         <mx:List id="Storys" dataProvider="{ac}" itemClick="{ss = Storys.selectedIndex}init();" x="10" y="25" width="175" height="299"/>
                         <mx:List id="Chapters" dataProvider="{ac2}" x="193" itemClick="storyText.text = Chapters.selectedItem + ' selected' " y="25" height="299"/>
                         <mx:Label x="10" y="10" text="Story"/>
                         <mx:Label x="193" y="10" text="Chapter"/>
                         <mx:Text x="363" y="25" width="201" height="299" id="storyText"/>
                         <mx:Label x="363" y="10" text="Content"/>
                         
                    
                    </mx:Application>
                    

                     

                    But copy that into a seperate application for testing, the XML file i parsed which is exact same to yours is located on www.snorksoft.co.uk/storys.xml

                     

                    Thanks! Rob.

                     

                     

                    Message was edited by: rbz.

                    • 7. Re: Remote mx:Model source not refreshing
                      Kaboom42 Level 1

                      Aha, I get it now. I need to change the format of the HTTPService, and there's a different syntax for extracting the data than I expected.

                       

                      I'll try it when I get time later, but I just wanted to thank you now for the quick and helpful response. If it works, you'll definitely get the "Correct Answer" flag!

                       

                       

                      EDIT: It works! I had to add the URL randomizer trick to prevent caching, but other than that it works to perfection. I then added texts to make it more user-friendly. Now all I have to do is type up all the chapters of my stories... three stories in progress, a total of 98 Chapters so far! Good luck to me... this is going to take some time.

                       

                      Anyway, thanks to all for your help!

                      • 8. Re: Remote mx:Model source not refreshing
                        rbz. Level 1

                        No problem!

                         

                        glad i could help, Good luck with the rest of the project!