PDA

View Full Version : Is it a good idea to create a Sequence Function for Observable constructor



r_honey
9 Jul 2010, 3:15 AM
I need to provide some custom functionality to all ExtJs Components on a Page, if they wish to avail it. I want a Component to indicate it in its config if it wants to avail this functionality via a simple boolean property, instead of each of them having to register explicitly for it.

To achieve this, I have added a sequence function to the Observable constructor:



Ext.util.Observable.prototype.constructor = Ext.util.Observable.prototype.constructor.createSequence(function() {
if (this.availFunctionality) {
//Do something
}
});


Is it a good idea to do so? Does someone see a problem in the code above, where I have replaced the Observable constructor with the return value of Function.createSequence method??

Animal
9 Jul 2010, 3:35 AM
The constructor is not in the prototype. The constructor IS Ext.util.Observable

So you cannot do that.

If you need to add some extra initialization to all Components, then you need to create a sequence from a method of Component.

r_honey
9 Jul 2010, 4:35 AM
The constructor is not in the prototype. The constructor IS Ext.util.Observable
So you cannot do that.

Well, I have already tried that (Ext.util.Observable.prototype.constructor = Ext.util.Observable.prototype.constructor.createSequence), and it works... The method is called whenever any Observable is instantized!!

r_honey
9 Jul 2010, 4:36 AM
Please note that the code above missed .prototype on the right hand of equals, which I later corrected (after the first reply by Animal above).

Animal
9 Jul 2010, 4:44 AM
The constructor is not in the prototype. The constructor IS Ext.util.Observable

So you cannot do that.

If you need to add some extra initialization to all Components, then you need to create a sequence from a method of Component.

r_honey
9 Jul 2010, 5:04 AM
Hmmm, exactly the same answer as before. If I recall correctly, there was a page somewhere that listed (diagramatically) the entire on method sequences in the life cycle. I can no longer locate that page. What I find is just an overview page, which does not provide much detail on Component's onXXX methods.

Animal
9 Jul 2010, 5:15 AM
initComponent

I tried to give you a clue.

Condor
9 Jul 2010, 5:49 AM
Unfortunately, Observable doesn't have an initComponent method.

The only way to modify the Observable constructor is to replace it, which is very complicated:

(function(){
var oldObservable = Ext.util.Observable;
// Create new Ext.util.Observable class with different constructor
Ext.util.Observable = Ext.extend(oldObservable.superclass || {}, Ext.applyIf({
constructor: function(){
oldObservable.apply(this, arguments);
// do stuff
}
}, oldObservable.prototype));
// Copy the class level methods (capture, observeClass etc.)
Ext.apply(Ext.util.Observable, Ext.applyIf({
prototype: Ext.util.Observable.prototype
}, oldObservable));
// Use the new Ext.util.Observable as superclass for all descendants
Ext.Component.superclass = Ext.util.Observable.prototype;
Ext.Direct.superclass = Ext.util.Observable.prototype;
Ext.Resizable.superclass = Ext.util.Observable.prototype;
Ext.SplitBar.superclass = Ext.util.Observable.prototype;
Ext.UpdateManager.superclass = Ext.util.Observable.prototype;
Ext.data.Connection.superclass = Ext.util.Observable.prototype;
Ext.data.DataProxy.superclass = Ext.util.Observable.prototype;
Ext.data.Node.superclass = Ext.util.Observable.prototype;
Ext.data.Store.superclass = Ext.util.Observable.prototype;
Ext.data.Tree.superclass = Ext.util.Observable.prototype;
Ext.dd.DragTracker.superclass = Ext.util.Observable.prototype;
Ext.direct.Provider.superclass = Ext.util.Observable.prototype;
Ext.form.BasicForm.superclass = Ext.util.Observable.prototype;
Ext.grid.AbstractSelectionModel.superclass = Ext.util.Observable.prototype;
Ext.grid.ColumnModel.superclass = Ext.util.Observable.prototype;
Ext.grid.GridView.superclass = Ext.util.Observable.prototype;
Ext.grid.PropertyStore.superclass = Ext.util.Observable.prototype;
Ext.list.ColumnResizer.superclass = Ext.util.Observable.prototype;
Ext.list.Sorter.superclass = Ext.util.Observable.prototype;
Ext.state.Provider.superclass = Ext.util.Observable.prototype;
Ext.tree.DefaultSelectionModel.superclass = Ext.util.Observable.prototype;
Ext.tree.MultiSelectionModel.superclass = Ext.util.Observable.prototype;
Ext.tree.TreeLoader.superclass = Ext.util.Observable.prototype;
Ext.util.ClickRepeater.superclass = Ext.util.Observable.prototype;
Ext.util.MixedCollection.superclass = Ext.util.Observable.prototype;
})();

Animal
9 Jul 2010, 6:02 AM
The OP wants to inject stuff into the initialization of Components.

It's as easy as



Ext.Component.prototype.initComponent = Ext.Component.prototype.initComponent.createSequence(function(){
console.log("Initializing ", this);
});

r_honey
9 Jul 2010, 6:54 AM
Thanks Animal and condor. I think initComponent should be a better option, and I am going with it.