Results 1 to 9 of 9

Thread: Problem extending Observable [Resolved]

  1. #1
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258

    Default Problem extending Observable [Resolved]

    Code:
    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.
    Last edited by evant; 28 May 2007 at 12:15 AM. Reason: Missed some stuff.

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890

    Default

    It looks like you're setting up the handler correctly with the scope. As long as you call

    Code:
        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.

  3. #3
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258

    Default

    Sorry, my bad, I was inadvertently creating a second instance in another place (hence the data being null).

    Works now, cheers.

  4. #4
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890

    Default

    Is your Linker a singleton? An object of which "there can be only one"?

  5. #5
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258

    Default

    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?

  6. #6
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890

    Default

    Yes.

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

  7. #7
    Sencha Premium Member
    Join Date
    Jul 2007
    Posts
    256

    Default

    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

    Code:
    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?

  8. #8
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854

    Default

    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.

  9. #9
    Sencha Premium Member
    Join Date
    Jul 2007
    Posts
    256

    Default

    Thanks, I looked at those components and followed that model.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •