8 Replies Latest reply on Oct 18, 2011 6:18 AM by sal00m

    Avoid binding behaviour

    sal00m

      Hi all,

       

      I'm trying to duplicate one object but when i modify the "master" object the child object is modified too. My code is something similar to this

       

      private function cloneLeaf():CallMonitoringLeaf{

                   var obj:CallMonitoringLeaf = new CallMonitoringLeaf();

                   obj.optionCallMonitoring = optionCallMonitoring;

                   obj.nodesSelected = nodesSelected;

                   obj.dataCallMonitoring = dataCallMonitoring;

                   obj.nodeName = nodeName;

                   return obj;

      }

       

      But when i modify nodeName (for example) in the object that i use to duplicate (master object), the child object modifies its name too. Is there any way to avoid this behaviour?

       

      Thanks in advance

        • 1. Re: Avoid binding behaviour
          Claudiu Ursica Level 4

          Unless your cloned properties are primitive types flex will pass them by reference and not value. I guess a deep copy with ObjectUtil.clone() will probably fix it.

           

           

          C

          • 2. Re: Avoid binding behaviour
            sal00m Level 1

            Thanks for the answer Claudiu

             

            The properties are flex primitives like string and ArrayCollection

             

            I've tried to clone the object using this function:

             

            public static function duplicateObject(sourceObject:*):*{

                                          var buffer:ByteArray = new ByteArray();

                                          buffer.writeObject(sourceObject);

                                          buffer.position = 0;

                                          return buffer.readObject();

                                }

             

            But the object returned it's not the same type (CallMonitoringLeaf) and the code doesn't work well

             

            I've tried to clone using ObjectUtil.copy and the behaviour is the same.

            • 3. Re: Avoid binding behaviour
              Claudiu Ursica Level 4

              Can you show come more code?

               

              C

              • 4. Re: Avoid binding behaviour
                sal00m Level 1

                Of course:

                 

                This is the class code:

                 

                package es.inr.ui.ivr.objects.leaves{

                          import es.inr.ui.ivr.objects.LeafChildWatcher;

                          import es.inr.ui.ivr.objects.leaves.formlayouts.CallMonitoringLeafForm;

                          import es.inr.utils.Constants;

                          import es.inr.utils.Util;

                          import es.inr.vo.ValidNode;

                 

                          import flash.events.ContextMenuEvent;

                          import flash.ui.ContextMenu;

                          import flash.ui.ContextMenuItem;

                 

                          import mx.collections.ArrayCollection;

                          import mx.controls.Alert;

                          import mx.styles.StyleManager;

                          import mx.utils.ObjectUtil;

                 

                 

                 

                          public class CallMonitoringLeaf extends LeafChildWatcher

                          {

                                    private var nodePicture:Class = StyleManager.getStyleDeclaration('.leavesGraphs').getStyle('nodeCallMonitoring');

                                    private var errorNodePicture:Class = StyleManager.getStyleDeclaration('.leavesGraphs').getStyle('errorNodeCallMonitoring');

                 

                                    public var optionCallMonitoring:String = "";

                                    public var channelSelected:String = "CALLER";

                                    public var dataCallMonitoring: ArrayCollection = new ArrayCollection;

                                    public var nodesSelected:ArrayCollection = new ArrayCollection;

                 

                 

                                    public function CallMonitoringLeaf(){

                                              this.source = this.nodePicture;

                                              this.type = Constants.LEAFTYPE_CALLMONITORING;

                                    }

                 

                override protected function updateContextMenu():void{

                                              var nodeMenu:ContextMenu = new ContextMenu();

                                                        nodeMenu.hideBuiltInItems();

                 

                                                var duplicateItem:ContextMenuItem = new ContextMenuItem(resourceManager.getString('App','basicNode.contextMenu.duplicate'));

                                                          duplicateItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelect Handler);

                                                          nodeMenu.customItems.push(duplicateItem);

                 

                 

                                              this.contextMenu = nodeMenu;

                  }

                 

                override protected function menuItemSelectHandler(e:ContextMenuEvent):void

                                    {

                                              switch(e.target.caption)

                                              {

                  case(resourceManager.getString('App','basicNode.contextMenu.duplicate')):

                                                                  getDesigner().addNode(getX() + 30, getY() + 30, cloneLeaf(),false);

                                                        break;

                                              }

                                    }

                 

                 

                                    override protected function getNodeForm():Object{

                                              return new CallMonitoringLeafForm();

                                    }

                 

                 

                                    private function cloneLeaf():CallMonitoringLeaf{

                                              var obj:CallMonitoringLeaf = new CallMonitoringLeaf;

                                              obj.channelSelected = channelSelected;

                                              obj.optionCallMonitoring = optionCallMonitoring;

                                              obj.nodesSelected = nodesSelected;

                                              obj.dataCallMonitoring = dataCallMonitoring;

                                              obj.nodeName = nodeName;

                                              return obj;

                                    }

                 

                 

                          } //Class CallMonitoringLeaf

                } // Package declaration

                 

                 

                If something else is needed just tell me

                • 5. Re: Avoid binding behaviour
                  Claudiu Ursica Level 4

                  I think cloneLeaf() is working properly.

                   

                  Bu to make sure debug it step by step.

                   

                  I assume that since is private the only place you are using it is inside the menuItemSelectHandler()

                   

                  However I don't reaaly see what should be happening in here from what you showed.

                   

                                                switch(e.target.caption)

                                                {

                    case(resourceManager.getString('App','basicNode.contextMenu.duplicate ')):

                                                                    getDesigner().addNode(getX() + 30, getY() + 30, cloneLeaf(),false);

                                                          break;

                                                }

                   

                  Inside this code you should have a clone of the object. Not sure what you actually do with the clone, and where you update the "master".

                   

                  C

                  • 6. Re: Avoid binding behaviour
                    sal00m Level 1

                    Exactly i have a clone of the object but with strong reference of the properties so, when i modify one of the objects the other its modified too, and i want to avoid this behaviour

                     

                    The getDesigner().addNode(getX() + 30, getY() + 30, cloneLeaf(),false) code, only add the cloned node to a canvas when i show all nodes (if need to paste some code i can paste it, but i think that doesn't matters in this problem)

                     

                    Adding some info:

                     

                    The data of the node is modified in one form (every node has is own form) and i display all nodes in a canvas (when i can move, drag drop, etc) and when i click right button i edit the form (as i said every node has is own form), modify properties and then write it to the object and when i edit one node the properties are writen to every object i clone from this, for example i clone one node 10 times, i edit one of it (not needed to be the "master") and all nodes are modified.

                    • 7. Re: Avoid binding behaviour
                      Claudiu Ursica Level 4

                      "

                       

                      Exactly i have a clone of the object but with strong reference of the properties so, when i modify one of the objects the other its modified

                      too, and i want to avoid this behaviour

                       

                      The getDesigner().addNode(getX() + 30, getY() + 30, cloneLeaf(),false) code, only add the cloned node to a canvas when i show all nodes (if need to

                      paste some code i can paste it, but i think that doesn't matters in this problem)

                       

                      Adding some info:

                       

                      The data of the node is modified in one form (every node has is own form)

                      and i display all nodes in a canvas (when i can move, drag drop, etc)

                      and when i click right button i edit the form (as i said every node has

                      is own form), modify properties and then write it to the object and when i edit one node the properties are written to every object i clone from

                      this, for example i clone one node 10 times, i edit one of it (not

                      needed to be the "master") and all nodes are modified.

                      "

                       

                      The thing is that yous said the nodeName which I assume is a String. It Should not modify, with the array collection is a different beast. They are objects and will be passed by reference...

                       

                       

                      Unfortunately, I still don't see the whole picture.

                      Let's say you start with one node. Where do you save the clone of the node? If you ca debug this and see that indeed you saved/cached a clone you're pretty much done.

                       

                       

                      C

                      • 8. Re: Avoid binding behaviour
                        sal00m Level 1

                        Got it, this give me the idea:

                         

                        "The thing is that yous said the nodeName which I assume is a String. It Should not modify, with the array collection is a different beast. They are objects and will be passed by reference..."

                         

                        The problem was about the arraycollection that its passed by reference, i used the previous duplicateObject function and clone this arrayCollection and it works.

                         

                        Thank you very much Claudiu