View Full Version : Question about inheritance
mankz
28 Sep 2009, 8:11 AM
I have a slightly messy scenario. I've created a CustomGridView class that extends the regular GridView. I now want a new GroupingView that inherits from my CustomGridView... Right now i've copy pasted the entire GroupingView and renamed it to say CustomGroupingView and made it extend my CustomGridView. I feel like I should be able to do some prototype magic to skip the ugly copy paste stuff... Ideas?
hendricd
28 Sep 2009, 8:17 AM
@mankz -- That's the approach I use with the ux.Media classes (they are deep and wide).
myGridView.Adapter = {
method1 : function(){},
method2 : function(){}
};
CustomGridView = Ext.extend(Ext.GridView, Object, myGridView.Adapter);
CustomGroupingView = Ext.extend(Ext.GroupingView, Object, myGridView.Adapter);
mankz
28 Sep 2009, 8:29 AM
Hmmm, not sure these scenarios are the same. I don't want to subclass groupingview. Just get a new groupingview on the same "level" as the regular groupingview. My new grouping view should have the exact same functionality as the regular grouping view. Just have another superclass.
Animal
28 Sep 2009, 9:48 AM
You mean the extra, added functions are the same?
Then keep a reference to the object parameter to the extend call.
myExtraMethods = {
refresh: function() {
...
},
renderRows: function() {
...
}
};
MyClass1 = Ext.extend(Ext.grid.GridView, Ext.apply({
functionSpecificToMyClass1: function() {
}
}, myExtraMethods));
MyClass2 = Ext.extend(Ext.grid.GroupingView, Ext.apply({
functionSpecificToMyClass2: function() {
}
}, myExtraMethods));
The blob that you supply to extend can be manipulated in any way, and can reference methods from anywhere.
mankz
28 Sep 2009, 10:03 AM
Ok, this is what I want and currently have since I copy/pasted the entire GroupingView class and just renamed it and made it inherit from My.CustomGridView:
Ext.grid.GridView
|
My.CustomGridView
|
My.CustomGroupingView
If I understand your proposal correct it would still end up something like:
Ext.grid.GridView
|
MyClass1
and
Ext.grid.GroupingView
|
MyClass2
My goal is ONLY to get MyClass2 having MyClass1 as its superclass (Without duplicating the code of course :) )
Animal
28 Sep 2009, 10:15 AM
That's right. I don't really understand what you want then.
mankz
28 Sep 2009, 10:22 AM
Hehe, sorry for confusing you. :) I tried dougs code but it didn't work out, I just feel I'm missing something essential. I just want a new groupingview that uses my own gridview as its superclass.
Animal
28 Sep 2009, 11:10 AM
What's wrong with extending your gridview class then?
mankz
28 Sep 2009, 11:49 AM
this is what I got:
Sch.CustomGridView = Ext.extend(Ext.grid.GridView, {
myFn1 : function() ...
});
and
Sch.CustomGroupingView = Ext.extend(Sch.CustomGridView, {
all the stuff copypasted from Ext's regular groupingview
});
Maybe this is the only way to do it. I just thought there would be an easier way to obtain what I want, without the ugly copy pasted code.
TommyMaintz
28 Sep 2009, 4:39 PM
I have not tested this, but you could try to do something similar to the following. Again, just typed this into the reply field, so might not be correct ;)
The Ext GroupingView doesnt seem to implement any custom constructor logic, so we can just skip that step.
Sch.CustomGroupingView = function(config) {
Sch.CustomView.constructor.call(this, config);
}
Ext.override(Sch.CustomGroupingView, Sch.CustomView.prototype);
Ext.override(Sch.CustomGroupingView, Ext.grid.GroupingView.prototype);
Ext.override(Sch.CustomGroupingView, {
// custom methods for your customgroupingview here
});
SamuraiJack1
4 Oct 2009, 11:49 PM
Thats a common situation in Ext, as I noticed. The very similar problem will arise, if you'll subclass a Store for example, and would like to use your subclass as JsonStore.
The solution:
http://joose-js.googlecode.com/svn/branches/mutability/doc/html/Joose/Manual/Roles.html
mankz
18 Oct 2009, 11:21 AM
This is the solution I use that works.
Sch.SchedulerGroupingView = Ext.extend(Sch.SchedulerView, {});
(function() {
var gvp = Ext.grid.GroupingView.prototype,
sgvp = Sch.SchedulerGroupingView.prototype;
for (var p in gvp) {
if (gvp.hasOwnProperty(p)) sgvp[p] = gvp[p];
}
})();
Mike Robinson
19 Oct 2009, 11:23 AM
Thats a common situation in Ext, as I noticed. The very similar problem will arise, if you'll subclass a Store for example, and would like to use your subclass as JsonStore.
The solution:
http://joose-js.googlecode.com/svn/branches/mutability/doc/html/Joose/Manual/Roles.html
:) "Holy Perl, Batman! I smell a Moose!" :)
SamuraiJack1
19 Oct 2009, 11:45 AM
:) "Holy Perl, Batman! I smell a Moose!" :)
:D
Updated link to manual: http://joose.github.com/Joose/doc/html/Joose/Manual.html
Mike Robinson
20 Oct 2009, 11:14 AM
I have been pondering how one might do "multiple inheritance" in JavaScript. Or "mixins" or what-have-you. But, preferably without introducing an entirely different metaphor into the ExtJS-centric mix.
"Moose I know." I confess, I haven't tried Joose yet, and I don't really know how well JavaScript will stand-up to such tomfoolery ;) vis-a-vis Perl. Is it reliable? Stable? Two-thumbs-up? Will I =D> or :((?
SamuraiJack1
20 Oct 2009, 1:35 PM
I have been pondering how one might do "multiple inheritance" in JavaScript. Or "mixins" or what-have-you. But, preferably without introducing an entirely different metaphor into the ExtJS-centric mix.
"Moose I know." I confess, I haven't tried Joose yet, and I don't really know how well JavaScript will stand-up to such tomfoolery ;) vis-a-vis Perl. Is it reliable? Stable? Two-thumbs-up? Will I =D> or :((?
Well, thats a philosophical question. How we can say for sure, whether the software is reliable, stable or its at least "just works"?:-?
We can only say whether it passes its own test suite or not (if it have one of course). Joose happens to have the test suite from ~600 tests which it perfectly passes on all browsers. B)
Recently I found a bug and fixed it. +4 new passing tests. I know that this bug won't be re-introduced anymore and Joose became a bit more stable and reliable. If you'll find a bug, publish it and Joose will became a bit more stable and reliable again.
Regarding this:
without introducing an entirely different metaphor into the ExtJS-centric mix.
Check this thread:
http://www.extjs.com/forum/showthread.php?t=55968
Especially this link:
http://symbie.org/sandbox/Ext3rc1-on-Joose/examples/
The whole /examples folder runs unmodified on bridged Ext. Thats one of the indirect examples of how Joose is reliable. As about direct examples - I'm using it (3.0 version) in all my projects (including $custom development). Only =D>
Bridge allows you to write your classes like this:
Class('Test.Run.Harness.Browser.UI.TestGrid', {
xtype : 'testgrid',
isa : Ext.grid.GridPanel,
does : ExtX.grid.Grouped,
have : {
harness : null
},
before : {
initComponent : function () {
this.addEvents('rowselect')
...
}
},
after : {
initComponent : function () {
this.getSelectionModel().on('rowselect', this.onRowSelect, this)
....
}
},
methods : {
onRowSelect : function (selModel, indx, record) {
this.fireEvent('rowselect', this, record)
}
}
})Also since you are a perl man, I can't resist to share this with you. In the next few weeks JSAN::Shell should be fixed. And it will be possible to type in command line:
> jsan2
Checking for Internet access...
Locating closest JSAN mirror...
jsan shell -- JSAN repository explorer and package installer (v2.0006)
-- Copyright 2005 - 2009 Adam Kennedy.
-- Type 'help' for a summary of available commands.
jsan>
jsan> install Joose
Resembles something, heh? ;)
Mike Robinson
21 Oct 2009, 6:58 AM
Heh. :D
"In the beginning was the Perl. By and through the Perl were All Things Made That Were Made."
"And the Windows was without form, and void. And darkness was upon the face of the Windows. Verily, some things have not changed since the beginning of time ..."
SamuraiJack1
21 Oct 2009, 7:14 AM
:))=D>
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.