Hi Fli7e,

This code looks complex to me, to provide such basic functionality.
Not "KISS" IMO, I prefer my "get" methods ;)
Anyway, this would fit better the other thread I've opened (http://
www.google.com/url?sa=D&q=http://groups.google.com/group/mootools-users/browse_thread/thread/7e3ce3a75a9f771&usg=AFQjCNEmzIOVds6a7bDIprS1MbSagZ_Vmg).

If Sean's solution works, to me it answers the question raised in this
topic.


On Nov 12, 12:57 pm, Fli7e <[email protected]> wrote:
> I exactly did what Aaron mentioned some weeks ago (auto creating
> getter Functions). This is what i came up with - works very well (for
> me):
>
> var myNewClass = new Class({
>         ...
>         options: {
>                 'selectors' : {},
>                 'styles' : {},
>                 'states' : {}
>         },
>
>         getOpt: function()
>         {
>         },
>
>         initialize: function( options )
>         {
>                 ...
>                 Object.each(this.options, function(optValue, optKey, 
> optObject){
>                         this.getOpt.extend(optKey.capitalize(), function(){
>                                 return this.options[optKey];
>                         }.bind(this));
>                 },this);
>         }
>
> });
>
> So when i do:
>
> var X = new myNewClass();
> console.log( JSON.encode( X.getOpt.Selectors() ) );
>
> i am getting the stuff i want ... in this case JSON encoded ...
>
> I used to add the Functions inside the getOpt Function (group
> function) to be able to add getter for different Objects with possible
> identical keynames to different function areas.
>
> no idea if this is (exactly) what Aaron was speaking about but it
> works like a champ for what i am using it.
>
> On 12 Nov., 12:06, Blackbird <[email protected]> wrote:
>
>
>
>
>
>
>
> > Hi Aaron,
>
> > I didn't experience the recursion error you're mentioning in 3).
>
> > Since your comments were very interesting and wanting to investigate
> > this further, I opened a new 
> > topic:http://groups.google.com/group/mootools-users/browse_thread/thread/7e...
>
> > On Nov 11, 10:28 pm, Aaron Newton <[email protected]> wrote:
>
> > > 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