View Full Version : Flexible / dynamic inheritance

03-17-2005, 07:30 PM
I have a problem in utilizing inheritance properly for the following scenario. The inheritance chain is not correct. Yes, I know JS does not do multiple inheritance (although you can kinda fake it), this is not a multiple inheritance problem, or is it?

I have a form with complex data relationships and I want to create objects to help corral the complexities. NOTE: We're using a "form building tool" called Liquid Office (LO), so I'm not working with HTML tags directly. But checkboxes, textboxes, etc. (hereafter referred to as "things") are exposed thru the LO interface.

I have 'mutually exclusive', 'mutually required' relationships etc. (hereafter refered to as "relationships"). Sometimes it's individual things that are related, sometimes it's groups of things that are related.

I want to be able to build individual things or sets of things and have relationships as needed. A "syncToMe(otherObject)" function will enforce the relationship. A "validate()" function will check for the relationship during form validation.

Wrap things as generic objects so that I have consistant functions "set()", unSet()", "enable()" etc. regardless of what kind of thing I'm dealing with. These methods are defined in the wrapper object and used in the relationship object

Next I want do define separate objects for each relationship. Each would have appropriate "validate()", "syncToMe()" functions for example.

Build individual "things" then "attach" the needed relationship to each thing. Thus I can enforce a relationship explicitly through either thing (or set of things) - for example each thing would invoke it's own "syncToMe()" function on it's "onClick" event.

I'm getting the feeling that I'm describing an abstract factory pattern, but not sure.

I'm including only one relationship, one thing, and an array of things but you get the idea.

function oMutuallyExclusive (Msg) {
this.errorMsg = Msg || "no Error Msg";

//don't override these functions
this.syncToMe = function (otherObject) {
if (this.isSet()) {

if (!this.isSet()) {
} // syncToMe()

this.validate = function (otherObject) {
var isValid = true;

if(this.isSet() && otherObject.isSet()) {
isValid = false;

if (!this.isSet() && !otherObject.isSet()) {
isValid = false;

return isValid;
} // validate()
} // MutualyExclusiveObjects Object

function oArraySet (theThings) {
this.theObjects = theThings;

this.set = function () {
for (i=0; i < theObjects.length; i++) {
} // set()

this.unSet = function () {
for (i=0; i < this.theObjects.length; i++) {
} // unSet()

this.enable = function (theState) {
for (i=0; i<theObjects.length; i++) {
} //enable()

this.isSet = function () {
var isSet = false;

for (i=0; i < this.theObjects.length; i++) {
if (this.theObjects[i].isSet()) {
isSet = true;
break nextObject;
return isSet;
} // isSet()
} //oArraySetObject()

//liquid office specific
function oCheckbox (theCB) {
this.box = theCB;

this.set = function (setting) {
} //set()

this.unSet = function (setting) {
} // unSet()

this.isSet = function () {
var isSet = true;

if (this.box.getValue() == 1) {
isSet = true;
isSet = false;

return isSet;
} // isSet()

this.enable = function (theState) {
} //enable
}// oCheckbox

Here is some code that expresses my intent. It does not work. The proper inheritance chain is not there.

var myCB = new oCheckbox(CSForm.getField("thecheckbox"));
var myText = new oTextbox (CBForm.getField("thetextbox"));

myCB.prototype = new oMutuallyExlcusive;
myText.prototype = new oMutuallyExlcusive;

// an onclick event would be handled like this: