A couple of general observations.

1) options should in principal be read-only (in my opinion). They aren't
where I prefer to keep state. I move that into either class properties
(this.foo = this.options.foo) or into a state object (this.state.foo =
this.options.foo).

2) If you want to serialize the options that instantiated the class, JSON
encode the options. If you want to serialize the state, serialize
this.state. Now you have the ability to recreate the class.

3) options can't ever have instances of other classes as properties; you'll
get a recursion error. If you want a class to have a reference to another,
add a setter method or a getter method in your options (this.fx =
this.options.getFx());

4) In your code example below, you have a lot of getters that are the same
pattern:

      getEndDateTime: function() {
               return this.options.endDateTime;
       },

This kind of repetition isn't necessary. Either write a .get method that
takes as its argument the option name or write a loop that loops over the
options and creates these getters. At least that's what I'd do.

-A


On Fri, Nov 11, 2011 at 1:28 AM, Blackbird <[email protected]> wrote:

> Hi,
>
>
> I'm using Mootools classes extensively in my business logic. I would
> like to encode only the "options" of corresponding instances in JSON.
> Automatically.
>
>
> Let me take an example. Encoding an instance of the following class:
>
> ########
> var Activity = new Class({
>    Implements: [Options],
>
>        initialize: function(options) {
>                this.options = options;
>        },
>
>        options: {  // Defaults
>                "vehicles": []
>        },
>
>        getId: function() {
>                return this.options.id;
>        },
>
>        getStartDateTime: function() {
>                return this.options.startDateTime;
>        },
>
>        getEndDateTime: function() {
>                return this.options.endDateTime;
>        },
>
>        getOriginStation: function() {
>                return this.options.originStation;
>        },
>
>        getDestinationStation: function() {
>                return this.options.destinationStation;
>        },
>
>        getComment: function() {
>                return this.options.comment;
>        },
>
>        getVehicles: function() {
>                return this.options.vehicles;
>        }
> });
> ########
>
>
> Now, calling "JSON.encode(myActivity)" will result in the following
> JSON:
>
> ########
> {
>    "options":{
>        "startDateTime":"2011-11-11T05:12:22.850Z",
>        "endDateTime":"2011-11-11T05:27:22.850Z",
>        "originStation":{
>            "$caller":null,
>            "caller":null,
>            "options":{
>                "code":"CST",
>                "name":"Stockholm C"
>            }
>        },
>        "destinationStation":{
>            "$caller":null,
>            "caller":null,
>            "options":{
>                "code":"CST",
>                "name":"Stockholm C"
>            }
>        },
>        "vehicles":[
>            {
>                "$caller":null,
>                "caller":null,
>                "options":{
>                    "id":"2110",
>                    "type":"RABe525"
>                }
>            }
>        ],
>        "trainNumber":"8424",
>        "tractionType":"M1"
>    },
>    "$caller":null,
>    "caller":null
> }
> ########
>
>
> But I would like only the "options" to be encoded, which would result
> in the following JSON:
>
> ########
> {
>    "startDateTime":"2011-11-11T05:12:22.850Z",
>    "endDateTime":"2011-11-11T05:27:22.850Z",
>    "originStation":{
>        "code":"CST",
>        "name":"Stockholm C"
>    },
>    "destinationStation":{
>        "code":"CST",
>        "name":"Stockholm C"
>    },
>    "vehicles":[
>        {
>            "id":"2110",
>            "type":"RABe525"
>        }
>    ],
>    "trainNumber":"8424",
>    "tractionType":"M1"
> }
> ########
>
>
> Is there a way to do this automatically?

Reply via email to