N
nick
Hello again! Today I have something a little more concrete for you
guys to muse over. In fact I doubt it will require much heavy musing
for some of you, being the prolific js experts you are.
Here's the
situation:
I have a UML class diagram I've made to describe the data model of a
web app I'm developing. I've written some XSL transformations to
generate SQL and PHP code from the model. Essentially, each class in
the model represents a table in the database, and a class in the PHP
model.
Each attribute in the model represents a column in the database (or a
linking table, for attributes with MTM relationships) and a property
of a class. All attributes are created as private properties, accessed
via a public Get_ and Set_ method created for each. If an attribute is
a complex type, the class will not instantiate it until the Getter is
called. If it's not instantiated, it can be retrieved from the DB at
that time.
Now I bet you're wonding, "what the heck does all this have to do with
javascript?" Well, I decided as long as I'm generating classes, I
might as well generate matching JS classes for use on the client. The
JS classes can have the same API as the PHP classes; the method
signatures can even match and the JS classes can make AJAX calls to
their counterpart PHP classes to set and retrieve data.
So, on with my question: What's the best pattern to use to create
these pseudoclasses? I'm particularly unsure of what to do about the
private properties, as from a few simple tests I've run "privileged"
methods with access to "private" properties (variables declared with
var, not properties of 'this') do not work so well once inheritance is
introduced. I'm seriously leaning toward making all the 'private'
properties private in name only... they are still accessible via
this.whatever, and just hope the programmers who end up working on
this thing use the GetWhatever and SetWhatever syntax instead.
Maybe a code sample will explain what I'm doing better. Here is one of
the generated classes. I've stripped out a bunch of jsdoc comments and
DAO methods, hopefully enough is left to give an idea of what I'm up
to.
....
//... following block is in another file ...
Function.prototype.extend = function(cls)
{
this.prototype = new cls;
this.prototype.constructor = this;
this.prototype._super = cls;
}
//... more generated stuff goes here...
Photo_Base.extend(GalleryApp);
this.Photo_Base = (function(){
// constructor
function Photo_Base()
{
this.Id = null;
this.Link = null;
this.Caption = null;
}
Photo_Base.prototype.GetId = function ()
{
return this.Id ? this.Id : 'null';
}
Photo_Base.prototype.SetId = function (value)
{
this.Id = value;
}
Photo_Base.prototype.GetLink = function ()
{
return this.Link;
}
Photo_Base.prototype.SetLink = function (value)
{
this.Link = value;
return this;
}
Photo_Base.prototype.GetCaption = function ()
{
return this.Caption;
}
Photo_Base.prototype.SetCaption = function (value)
{
this.Caption = value;
return this;
}
return Photo_Base;
})(); // end of class Photo_Base.
....
So, how does this design look? Will it work, or do I need to take this
back to the drawing board? Is there some trick that will allow me to
use "protected" variables (say, 'var Caption' instead of
'this.Caption') that will not stay attached to their old class scope
when the class is extended? I hope this makes at least some sense.
Thanks again, guys,
-- Nick
guys to muse over. In fact I doubt it will require much heavy musing
for some of you, being the prolific js experts you are.
situation:
I have a UML class diagram I've made to describe the data model of a
web app I'm developing. I've written some XSL transformations to
generate SQL and PHP code from the model. Essentially, each class in
the model represents a table in the database, and a class in the PHP
model.
Each attribute in the model represents a column in the database (or a
linking table, for attributes with MTM relationships) and a property
of a class. All attributes are created as private properties, accessed
via a public Get_ and Set_ method created for each. If an attribute is
a complex type, the class will not instantiate it until the Getter is
called. If it's not instantiated, it can be retrieved from the DB at
that time.
Now I bet you're wonding, "what the heck does all this have to do with
javascript?" Well, I decided as long as I'm generating classes, I
might as well generate matching JS classes for use on the client. The
JS classes can have the same API as the PHP classes; the method
signatures can even match and the JS classes can make AJAX calls to
their counterpart PHP classes to set and retrieve data.
So, on with my question: What's the best pattern to use to create
these pseudoclasses? I'm particularly unsure of what to do about the
private properties, as from a few simple tests I've run "privileged"
methods with access to "private" properties (variables declared with
var, not properties of 'this') do not work so well once inheritance is
introduced. I'm seriously leaning toward making all the 'private'
properties private in name only... they are still accessible via
this.whatever, and just hope the programmers who end up working on
this thing use the GetWhatever and SetWhatever syntax instead.
Maybe a code sample will explain what I'm doing better. Here is one of
the generated classes. I've stripped out a bunch of jsdoc comments and
DAO methods, hopefully enough is left to give an idea of what I'm up
to.
....
//... following block is in another file ...
Function.prototype.extend = function(cls)
{
this.prototype = new cls;
this.prototype.constructor = this;
this.prototype._super = cls;
}
//... more generated stuff goes here...
Photo_Base.extend(GalleryApp);
this.Photo_Base = (function(){
// constructor
function Photo_Base()
{
this.Id = null;
this.Link = null;
this.Caption = null;
}
Photo_Base.prototype.GetId = function ()
{
return this.Id ? this.Id : 'null';
}
Photo_Base.prototype.SetId = function (value)
{
this.Id = value;
}
Photo_Base.prototype.GetLink = function ()
{
return this.Link;
}
Photo_Base.prototype.SetLink = function (value)
{
this.Link = value;
return this;
}
Photo_Base.prototype.GetCaption = function ()
{
return this.Caption;
}
Photo_Base.prototype.SetCaption = function (value)
{
this.Caption = value;
return this;
}
return Photo_Base;
})(); // end of class Photo_Base.
....
So, how does this design look? Will it work, or do I need to take this
back to the drawing board? Is there some trick that will allow me to
use "protected" variables (say, 'var Caption' instead of
'this.Caption') that will not stay attached to their old class scope
when the class is extended? I hope this makes at least some sense.
Thanks again, guys,
-- Nick