merging static and dynamic data in model

andreas's Avatar

andreas

11 Oct, 2011 04:27 PM

Hi,

what is the best practice for merging static data that will be compiled into the SWF and dynamic data that comes from a server?
If we have for example a list of food that is static {name: Apple, type: fruit, color: red/green, cost: 0.99, weight: 1}, {name: Orange, type:fruit, color: orange, cost: 0.66, weight: 0.5}
and a Service system that represents the items in my basket {db_id: 1, name: Apple, amount: 5}, {db_id: 2, name: Orange, amount: 10}
how would my Model and VO look like? Where do I merge the static data and the data from the server?

  1. 1 Posted by Pierre Laveklin... on 12 Oct, 2011 11:01 AM

    Pierre Laveklint's Avatar

    Hi Brean,

    First of, when you say "merge", are you talking about actually merging the values of the server-objects with your static objects? To me it looks like the server-objects are "parents" to each fruit-name if you see what I mean. They just holds the amount of Apples, Oranges etc and an id representing a row id in a database.

    So it kinda depends on if you want to increase/decrease the 'amount' value in the server-objects based on your static objects or actually merge the data?

    Can you give us some more info?

  2. 2 Posted by andreas on 12 Oct, 2011 12:18 PM

    andreas's Avatar

    Can you give us some more info?

    OK, how about some code:
    first the file that contains the static data:
    data/fruit/AppleData.as

    public class AppleData {
         public static var name:String = "Apple";
         public static var cost:Number = .99;
    }
    

    and the VO for data from the server:
    model/vo/Fruit.as

    public class Fruit {
        public var name:String;
        public var amount:uint;
    }
    

    So, Data from the Server comes in to a service that creates a new Fruit-Instance, but how do i get the data of the AppleData-class into this (what I called a merge)?
    1. I could add a new variable to the Fruit-VO public var fruitData:Object and map the right fruit-class to the Fruit-VO in the Model and do the calculation fruit.fruitData.cost * fruit.amount in the skin of the list where the data is shown.

    The mapping looks something like fruitData = allFruitData[fruitVo.name] (allFruitData looks something like allFruitData:Dictionary = {apple: AppleData, orange: OrangeData};)
    2a. create a second Fruit class in the Mediator that contains an instance of both classes that has a getter for the view:

    class FruitMerge {
        public var fruitVo:Fruit;       //fruitVo.name = "apple"
        public var fruitData:Object   //instance of AppleData
    
        function get total():Number {
            return fruitVo.amount * fruitData.cost
        }
    }
    

    so the Mediator would do the mapping of the data-object and the vo.
    2b. to the same as 2a. but in a Command: listen to the cration of new VOs, create a new FruitMerge-Object containing the Fruit-VO and the FruitData that will be handed over to the Mediator.

    So what is the best practice for this?

  3. 3 Posted by andreas on 12 Oct, 2011 12:43 PM

    andreas's Avatar

    Pierre, I just realized I did not really answer your question, I also did not know what you mean by parent... so, the data from the server comes directly out of a database that only knows the name, the id and the amount. All other data is static and hosted by another system (in the real system I generate the code of for Data-Objects so I can compile them into the swf that can be cached by the browser and minimize the data that comes from the server to save customer bandwith).

  4. Ondina D.F. closed this discussion on 21 Nov, 2011 09:13 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