7 Replies Latest reply on Dec 14, 2009 10:51 PM by Shivik

    Problem in updating views using presentation models

    Shivik

      Hello all

       

      I am trying to implement presentation model using Cairngorm in my flex app. I am having problem in updating views using presentation model.

       

      Here's what I am doing.

       

      1. I am trying to create a login screen.
      2. To store the current user logged in I created a "currentUser" property in ModelLocator.
      3. My member info screen (the screen displayed after successful login) is displaying the name of user like this.
        • <mx:Label label="{memberUI.fullname}" />
      4. I created a presentation model, memberUI for the member info screen. This has a property called "fullname", which stores the fullname of the current user.
      5. I created a method in presentation model called setFullname() which does the job of setting the fullname property. This function is being called in the responder for login event.

       

           public function setFullname():void {
                var appModel:AppModelLocator = AppModelLocator.getInstance();
                appModel.currentUser = event.result as UserVO;
                var uim:memberUI = new memberUI();
                uim.fullname = appModel.currentUser.firstname + " " + appModel.currentUser.lastname;
           }
      

       

      But even when I successfully login, I am unable to display the fullname in the view. Why is this happening?

       

      Can somebody tell me what am I doing wrong?

       

      Thanks and Regards

      ShiVik

        • 1. Re: Problem in updating views using presentation models
          paul.williams Level 4

          The setFullname() method is creating a memberUI instance and assigning it to a local variable, so it isn't clear how the view could be binding to it. Perhaps your view is binding to the wrong instance of memberUI?

          1 person found this helpful
          • 2. Re: Problem in updating views using presentation models
            Shivik Level 1

            Hello there Paul

             

            here is what's happening in memberUI.as - the presentation model

             

            package com.mycompany.products.models
            {
                 [Bindable]
                 public class memberUI {
                      public var fullname:String;
            
                      public function memberUI() {}
                      public function setFullname() {
                           var appModel:AppModelLocator = AppModelLocator.getInstance();
                           this.fullname = appModel.currentUser.firstname + " " + appModel.currentUser.lastname;
                      }
                 }
            }
            

             

            In my view - memberMenu.mxml

             

            I did this in the Script tag,

             

            [Bindable]
            public var mui:memberUI = new memberUI();
            

             

            and bound it like this

             

            <mx:Label labe="{mui.fullname}" />

             

            The view doesn't update. The label for fullname displays nothing. I am calling the setFullname method in responder like this...

             

            public function result( _data:Object ):void {
                 var aModel:AppModel = AppModel.getInstance();
                 aModel.currentUser = _data.result as UserVO;
                 var mui:memberUI = new memberUI();
                 mui.setFullname();
            }
            

             

            Thanks and Regards

            ShiVik

            • 3. Re: Problem in updating views using presentation models
              paul.williams Level 4

              What is calling setFullname() and how does it get a reference to the memberUI instance that you created in the view?

               

              Have you debugged / tested the setFullname() function to ensure it is setting the fullname property as expected?

              1 person found this helpful
              • 4. Re: Problem in updating views using presentation models
                Shivik Level 1

                Hello Paul

                What is calling setFullname() and how does it get a reference to the memberUI instance that you created in the view?

                The setFullname function is being called in the responder like this...

                 

                 public function setFullname():void {
                          var appModel:AppModelLocator = AppModelLocator.getInstance();
                          appModel.currentUser = event.result as UserVO;
                          var uim:memberUI = new memberUI();
                          uim.fullname = appModel.currentUser.firstname + " " + appModel.currentUser.lastname;
                     }

                But this again isn't correct. Since I create a new instance of presentation model in the view, so the label in the view goes null.

                 

                How should I update the properties of presentation models? I know now that this tactic of calling a function of presentation model in responder won't work. Like how should I update the fullname of user in presentation model when I get a successful login in responder?

                 

                Can you shed some light here?

                 

                With kind regards

                ShiVik

                • 5. Re: Problem in updating views using presentation models
                  paul.williams Level 4

                  I've seen two approaches for this within PM + Cairngorm architectures.

                   

                   

                  1) Pass a reference of your PM into your Cairngorm event to allow the command / responder to update it with the results of a service call. This is a simple approach but does tie your commands and responders to your PM class, making them less versatile.

                   

                   

                  2) Have your PM register callbacks with your Cairngorm event to allow results to be propagated back to your PM. The callbacks could be in the form of event listeners, function references or an object implementing IResponder.

                   

                   

                  Unfortunately I am not aware of many examples online for this, but the following link achieves (2) by passing function references:

                   

                   

                  http://www.allenmanning.com/?p=31

                  • 6. Re: Problem in updating views using presentation models
                    Shivik Level 1

                    Thank you Paul for guiding me in the right direction. The post you mentioned will surely help.

                     

                    Thanks once again

                    ShiVik

                    • 7. Re: Problem in updating views using presentation models
                      Shivik Level 1

                      Hello there Paul

                       

                      I looked a bit at using the callback functions in the responding command. That works well. But I am stuck at what's going to happen when I separate the responder and command ( which I usually do ). Then how will I be able to call the callback function registered in event since the responder doesn't have any idea which event it belongs to.

                       

                      With kind regards

                      ShiVik