PDA

View Full Version : Merging properties from subclasses rather than overriding?



brian428
13 Jul 2012, 12:33 PM
Hi, I couldn't find an obvious way to do this so I thought I'd ask:

Say that Class B extends Class A. Class A defines a property "myProperty" which defines an object. What I'd like to have happen is that if Class B also defines a "myProperty" object, rather than just have that object completely override the object in Class A, I'd like to get a merged version of the of myProperty in Class A.

Taken further, if I had Class C that extended B, what I'd want to end up with in Class A is the merged version of the object from C, B, and A.

I know this is possible if I define the values in the constructors and pass it up using callParent() so that that Class A can use merge() or apply() and set the property. But I'm trying to keep the configuration for people creating the subclasses as simple as possible, so it would be great if they could just define it as a property and have the merging happen transparently.

Any idea if such a thing is possible?

Thanks,

Brian

skirtle
13 Jul 2012, 4:14 PM
It is but it's tricky. Ext.data.Model does what you're describing with the fields property, merging the fields for model subclasses. I haven't studied it in enough details to provide a simple example but hopefully you'll be able to figure it out by looking through the source.

brian428
14 Jul 2012, 9:58 AM
Thanks, it's a start! I'll take a look at the source.