Creating a temporary data store in a mediator

stephenadams1's Avatar

stephenadams1

03 Nov, 2011 12:33 PM

Hi,

I'm creating a panel in a AIR app in which users add information then when the user saves the form all the details in the form are added to the model and the 'header' text of the form is displayed in another panel. Then the user can reuse the form to add a new set of data.
Also the user can select a entry from the other panel and this fills the first panel with all the saved details, so they can update the information.

To store this info I'm using a Value Object, which I add to the main models array collection when I save the details in the panel, then to update the details I find the Value Object within the ArrayCollection and pass that to the mediator to populate the view.

The problem I'm having is that I sometimes get a mix of data. Is there a cleaner method of creating this type of application?

Stephen

  1. 1 Posted by andreas on 03 Nov, 2011 01:57 PM

    andreas's Avatar

    I am not quite sure what you mean by "mix of data".
    I think a better way would be to use a service that updates your model after the user saves the data instead of letting the mediator modify the model (also this way you make sure model and your saved data are always the same).
    I also think that it is the job of the view to know which vo it is showing (correct me, if I am wrong, I am not so sure about this).

  2. 2 Posted by stephenadams1 on 03 Nov, 2011 02:20 PM

    stephenadams1's Avatar

    Hi,

    When I said 'mix of data' what happens is that I have an array within this Value Object which sound time shows the previous list of items in the array.

    What is not happening is that the Value object that manages the data of the view is not getting cleared out complete, which I know I could do this using:

    myArray = [];

    This causes the array to be null if I try to add new items to it.

    What I want to do is:

    1. Save the details entered in a form to a Value Object
    2. Add that Value Object to the model
    3. Create a new Value Object and add that to the model
      3a. Load a existing Value Object from the model
      3b. Amend the details in the loaded Value Object
      3c. Update the match Value Object in the model

    Stephen

  3. Support Staff 3 Posted by Ondina D.F. on 04 Nov, 2011 10:59 AM

    Ondina D.F.'s Avatar

    Hi Stephen,

    I, too, would like to help you find a solution, but I have a train to catch in a few hours.
    I’m sure someone else will chime in with an answer or suggestion.
    You could also search the forum for “vo in view”.

    Ondina

  4. 4 Posted by stephenadams1 on 04 Nov, 2011 11:08 AM

    stephenadams1's Avatar

    Hi,

    I will, thanks for advice, good luck catching your train.

    Stephen

  5. Support Staff 5 Posted by Stray on 04 Nov, 2011 11:17 AM

    Stray's Avatar

    Hi Steven,

    Could you start again and explain it a little more slowly? I'm a bit confused - and I think part of that confusion is just a few typos (there's a "sound time" which I can't work out), and partly I'm not sure why:

    myArray = [];
    

    Would cause the array to be null - because that's the syntax for creating a new, empty array.

    The thing that most immediately springs to mind is that I usually find that when I'm working with VOs and models I have to make a copy of any array I make changes to using slice() because Arrays are passed by reference, not value. So any updates you make to an array passed by value will immediately update the data in the original item as well, which usually isn't what I want to happen.

    But that may be unrelated.

    So - tell us what currently happens, exactly what you're seeing and what you're expecting to see, and I'm sure we can figure it out with you.

    Stray

  6. 6 Posted by stephenadams1 on 04 Nov, 2011 12:07 PM

    stephenadams1's Avatar

    Hi,

    Ok, here's a explanation of what I'm trying to do, hope it helps.

    I'm creating a extension for Creative Suite, it has two panels, one above the other. The top panel is a data entry form, the second panel shows a list of the saved details from the top panel.

    The way I have this working is, a value object (VO), which contains a string and a array. Is being used to store/package the details of the form being created.

    A user completes the form by entering a title it a textbox, this is stored in the string value in the VO. They then select items from the document they are working on in the Creative Suite application.

    Every time the user selects a item they click a 'add' button, this gets the name of the selected item and adds it to the array of the VO. It also causes the view to display the name in a textbox, every time they click on the add button a new textbox is added to the panel.

    Once they have added all the selected items, they click the 'save' button. This fires a event that sends the VO to the application model.

    The application model contains a Array Collection, which VO are added or removed.

    When a VO is added to the model, the name (the string value in the VO) is displayed in the second panel. Every time a new VO is added to the model the name is displayed in a list in the bottom panel.

    When the save button is click the top panel is cleared, this means the name textbox is cleared and the added textboxes that show the names of all the selected items, are removed.

    The user then has 2 choices either enter a new name and select new items from the CS document or select a name from the bottom panel list to edit a save VO.

    The problem I'm having is if the user decides to enter the details of a new item in the top panel, when they select a new item from the CS document the previous set of items are also shown, with the new item. It appears that the array is not being cleared.

    So I added the function that is called when the save button is clicked, which clears the form (it empties the name textbox and removes all the added item textboxes).

    In this 'clearForm()' function I set the array to:

    myValueObject.myArray = []

    This works, but when I later then select a saved item name from the bottom panel (this should populate the top panel with all the details of the saved VO from the models ArrayCollection) only the name is returned but the items array is not. So the user does not see the list of items they have selected from the CS document they are working with.

    If I don't clear the array like this, then when the user tries to add new items to a new entry in the top panel, all the existing items from the previously saved VO are shown.

    Really all I want to do is:

    1. enter details in a form
    2. save these details
      3a. enter a new set of details and save them
      3b. select a already saved set of details, view them and update them

    Sounds simple, but somewhere I have gone wrong.

    Does this helps?

    Stephen

  7. 7 Posted by Stray on 04 Nov, 2011 12:11 PM

    Stray's Avatar

    Hi Stephen,

    when you do

    myValueObject.myArray = []

    Is the myValueObject a new one, or might it still be referencing the same object that you were using before?

    Stray

  8. 8 Posted by stephenadams1 on 04 Nov, 2011 12:18 PM

    stephenadams1's Avatar

    Hi,

    In my view I have a temp array that stores all the selected items name, this is then set to the array in the value object like this:

    myValueObject.myArray = myTempArray;

    then on the save function which is called by the save button I have a function which clears the form and sets this temp array like this

    public function clearForm():void

            {
                this.nameTxt.text = "";
                this.myTempArray = [];
                this.childrenTextboxContainer.removeAllChildren();
                this.saveBtn.enabled = false;
                this.updateBtn.enabled = false;
            }
    

    Stephen

  9. 9 Posted by Stray on 04 Nov, 2011 12:26 PM

    Stray's Avatar

    Hi Stephen, I'm afraid I've got no idea what's causing your problem then.

    Is the view coming and going from the stage? Or is it always visible?

  10. 10 Posted by stephenadams1 on 04 Nov, 2011 12:30 PM

    stephenadams1's Avatar

    Hi,

    Its always visible, its a AIR app that runs inside a Creative Suite

  11. Support Staff 11 Posted by Ondina D.F. on 04 Nov, 2011 01:54 PM

    Ondina D.F.'s Avatar

    Stephen, just a quick observation or rather a few questions before heading to the train station:

    Is the VO in your view the same as the one you’re manipulating in your model? You are talking about adding and removing VOS in the model, but how are you refreshing the list with the new VOS? So there isn’t just one vo, but several vos?
    You are emptying the VO’s array in the view, but it seems that the method in your view, which is responsible for what you described here:
    “if the user decides to enter the details of a new item in the top panel, when they select a new item from the CS document” is picking the old VO. So I’d try to see if there is a need for doing something like this: myVO:SomeVO=new SomeVO(), or if you want SomeVO to be always the same and just empty its vars.
    Maybe you should tell Stray more about the structure of your models and vos and paste in here the methods from your view where you are using these vos.

    Ondina

  12. Support Staff 12 Posted by Ondina D.F. on 04 Nov, 2011 02:49 PM

    Ondina D.F.'s Avatar

    Try this:
    1. myValueObject.myArray = myTempArray;
    2. clearForm: this.myTempArray = [];
    and then :
    3. myValueObject.myArray = [];

  13. 13 Posted by stephenadams1 on 11 Nov, 2011 12:09 PM

    stephenadams1's Avatar

    Hi,

    I was wondering if the problem is where my ArrayCollection is being instantiated everytime I call my save function.

    I have mapped my model as a singleton, like this:

    injector.mapSingletonOf( IDynamicGroupsModel, DynamicGroupsModel );

    Inside that I create a ArrayCollection like this:

    private var dynamicGroupCollection:ArrayCollection = new ArrayCollection();

    then my save function looks like this:

    public function saveGroup(dynamicGroup:DynamicGroupVO):void

        {
            dynamicGroupCollection.addItem( dynamicGroup );
            groupSaved();
        }
    

    Is there a way I can map the ArrayCollection as a singleton, so that only the one instance of the array collection is used.

    Stephen

  14. 14 Posted by Stray on 11 Nov, 2011 12:16 PM

    Stray's Avatar

    Hi Stephen,

    I wouldn't map the collection itself, but if you create a simple wrapper class that just holds the array collection, you can map this as a Singleton.

    But if you've only got one instance of it inside the DynamicGroupsModel then this is guaranteed to be single.

    So - can't you just make sure the collection is only instantiated once inside the model? Just make it a member of that model and its 'singleness' will be ensured.

    Stray

  15. 15 Posted by stephenadams1 on 11 Nov, 2011 12:20 PM

    stephenadams1's Avatar

    Cool, thanks.

    That means I can rule that problem out.

    Stephen

  16. Ondina D.F. closed this discussion on 13 Nov, 2011 08:25 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac