something like this ...

var Stations = {};
var Vehicles = {};

var Activities = new Class({
        Implements: [Options],
        'options' : {
                'startDate' : null,
                'endDate' : null,
                'station' : null,
                'vehicles' : []
        },
        initialize: function( options )
        {
                this.setOptions(options);
        },
        getOptions: function()
        {
                return JSON.encode( this.options );
        },
        getOpt: function( optKey )
        {
                return this.options[optKey];
        },
        setOpt: function( optKey, optValue )
        {
                this.options[optKey] = optValue;
                return this;
        },
});


On 12 Nov., 12:01, Blackbird <[email protected]> wrote:
> Hello,
>
> I don't really know the best way to code entity classes in Mootools.
>
> Let's consider the following entities:
>
> - Activity:
> Contains the following properties:
>         ¤ startDate (of type date)
>         ¤ endDate (of type date)
>         ¤ station (of type Station entity)
>         ¤ vehicles (list of Vehicle entities)
>
> - Station
>         ¤ code (string)
>         ¤ name (string)
>
> - Vehicle
>         ¤ id (string)
>
> I need:
>
> a) To be able to set properties at instantiation, .NET style. For
> example:
> ########
> var break = new Activity ({
>         "startDate": Date.parse("2011-4-12 21:45:00"),
>         "endDate": Date.parse("2011-4-12 22:00:00"),
>         "station": Stations.stockholm,
>         "vehicles": [ Vehicles.v2110, Vehicles.v2111 ]});
>
> ########
>
> b) To easily encode this Activity object (+ children) in JSON, which
> would result in:
> ########
> {
>     "startDate": "2011-4-12 21:45:00",
>         "endDate": "2011-4-12 22:00:00",
>         "station": {
>             "code": "CST",
>             "name": "Stockholm C"
>         },
>         "vehicles": [ {
>                 "id": "v2110"
>         }, {
>                 "id": "v2111"
>         }
>         ]}
>
> ########
>
> c) To have a clean "public" interface to get and set an object's
> properties, hiding the underlying implementation. For example:
> ########
> "var breakStartDate = break.startDate" + "break.startDate = newDate"
>
> "var breakStartDate = break.getStartDate()" +
> "break.setStartDate(newDate)"
> ########
>
> I'm sure many of you have faced this problem. How do you handle it?

Reply via email to