Saturday, March 24, 2012

Question about Overriding Methods

Greetings,
I'm having trouble seeing the base class method if I've overriden it...I am not sure what I'm doing wrong. I've tried to study how the Atlas framework is using it, but nothing is working. Here's my code:
Type.registerNamespace("Juicy.Collections");
Juicy.Collections.ArrayList = function() {
var _items = new Array();
this.get_item = function(i) { return _itemsIdea [I]; }
this.get_length = function() { return _items.length; }
this.add = function(item) { _items.add(item); }
this.clear = function() { _items.clear(); }
this.dispose = function() { for (var i = 0; i < _items.length; i++) { _itemsIdea [I].dispose(); _itemsIdea [I] = null; } _items.clear(); }
}
Type.registerClass("Juicy.Collections.ArrayList", null, Web.IDisposable);
Type.registerNamespace("Juicy.UI");
Juicy.UI.TabPageCollection = function(owner) {
Juicy.UI.TabPageCollection.initializeBase(this);
var _owner = owner;
this.add = function(tabPage) { Juicy.UI.TabPageCollection.callBaseMethod(this, "add", tabPage); } // The callBaseMethod always evaluates to null.
Juicy.UI.TabPageCollection.registerBaseMethod(this, "add"); }
Type.registerClass("Juicy.UI.TabPageCollection", Juicy.Collections.ArrayList);
// Code to test the above. The get_length() works fine, but the add does not increment properly.
var tabPages = new Juicy.UI.TabPageCollection();
tabPages.add("foo");
alert(tabPages.get_length());
I want the "add" method in TabPageCollection to do some work, and then call the inherited add. But the callBaseMethod does not resolve.
Any ideas anyone? This is driving me nuts here.
Thanks,
William

I think I found the answer to my question. The base class has to have a call to "registerBaseMethod" for the method that may be overridden.
Just as an idea, wouldn't it be more intuitive for the derived class to have syntax/method indicating that it is overriding the base? I can't off the top of my head think of how this could be done, but it took me awhile to figure out how the current manner works. Perhaps Type.overrideMethod(this, methodName, someFunctionReference).
Thanks,
William

You need to register the base method in the ArrayList class.

Juicy.Collections.ArrayList.registerBaseMethod(this, 'add');

Then your callBaseMethod should start resolving. However, you need to make one more small tweak - the arguments to the base method are passed as an array of arguments. So you would have:

Juicy.UI.TabPageCollection.callBaseMethod(this, 'add', [tabPage]);

No comments:

Post a Comment