PDA

View Full Version : Problem extending Observable [Resolved]



evant
28 May 2007, 12:03 AM
Code:


Linker = function()
{
this.dlg = null;
this.data = null;
this.addEvents({'complete': true});
Linker.superclass.constructor.call(this, {});
};

Ext.extend(Linker, Ext.util.Observable,
{
retrieve: function()
{
this.fireEvent('complete', this, this.data);
},
show: function()
{
//some code to create a dialog
this.dlg.addButton('Save', this.retrieve, this);
this.dlg.addButton('Close', this.dlg.hide, this);
//show dialog
}
}
);


When retrieve() is called after save is pressed, this.data always returns null because this is referring to the save button object (as opposed to the current instance). I think I'm not setting up the event handlers for the dialog correctly?

I've had a look at some of the Ext code and noticed that there doesn't seem to be any issue using this inside the class, so I've obviously screwed something up.

Your help would be appreciated.

Animal
28 May 2007, 12:09 AM
It looks like you're setting up the handler correctly with the scope. As long as you call



myLinker.show()


Then that will be OK.

I think "data" is null because you set it to null in the constructor, and never set it to any value.

evant
28 May 2007, 12:15 AM
Sorry, my bad, I was inadvertently creating a second instance in another place (hence the data being null).

Works now, cheers.

Animal
28 May 2007, 12:17 AM
Is your Linker a singleton? An object of which "there can be only one"?

evant
28 May 2007, 12:18 AM
No, although I'm thinking that perhaps it should be now.

I'm looking at the QuickTips code now for an example of a singleton. Is it possible to have an Observable singleton?

Animal
28 May 2007, 12:55 AM
Yes.



MyObservableSingleton = function(){
return Ext.apply(new Ext.util.Observable (), {
init: function() {
},

doStuff: function() {
}
});
}();

brookd
8 Nov 2007, 3:57 PM
First, I am very new to JS and OOP.

How can you create a new instance of this class? I tried:

myInstance = new MyObservableSingleton();

But that did not work. I think my problem is about what class design works the best with EXT. I was using a format like this for JS classes




var Class = {
create: function() {
return function() {
this.init.apply(this, arguments);
}
}
}

formEditorController = Class.create();

formEditorController.prototype = {

init: function(cfg)
{
}



With this design, I can create instances of this class by calling:

new formEditorController({'foo':true})

But I could not figure out how to extend this class to use observable. What class design should I use for classes that will have multiple instances and I also want to be able to extend EXT classes?

tryanDLS
8 Nov 2007, 4:51 PM
If you want to see examples of how create classes that extend Observable, I would suggest you look at Component, which extends Observable, and then BoxComponent which extends Component. This pattern is common throughout Ext.

brookd
9 Nov 2007, 4:39 PM
Thanks, I looked at those components and followed that model.