Results 1 to 9 of 9

Thread: Listeners and Scope

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Ext User
    Join Date
    Mar 2008
    Posts
    90
    Vote Rating
    0
      0  

    Default Listeners and Scope

    Hi All,

    I am trying to fire an event in my JS file to a listener rendered by lazy-initialization code, but the two don't want to talk to each other. Do I need to define a specific scope? They are both rendered on the same page..

    JS
    Code:
    	this.addEvents({'checkactive': true});
    
            .....
    
    	this.grid.on('rowclick',  function(grid, index, e) {		
    		if (sm.getCount() > 0) {
    			this.fireEvent('checkactive', 'enabled');						
    		} else { 
    			this.fireEvent('checkactive', 'disabled');						
    		}
    	}, this);
    In my JSON lazy initialization code:

    Code:
    	buttons: [{
    		"text": "Submit",
    		"type": "button",
    		"scope": this,
    		"handler": function(){ submit(); },
    		"listeners": {
    	  			 "checkactive": function(isenabled) {
    						alert(isenabled);				
    				},
    				scope:this
    		}
               .....
    I've checked in FireBug and the fireEvent is run, but the listener never gets executed.

    Any ideas?

    Thanks

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Redwood City
    Posts
    30,626
    Vote Rating
    54
      1  

    Default

    this.grid.on('rowclick'....

    You bind that handler function to whatever "this" is at that time. That is then what the 'checkactive' event is fire on.
    Longtime Sencha geek. Outspoken advocate of pure Javascript Views. Posts my own opinions.

  3. #3
    Ext User
    Join Date
    Mar 2008
    Posts
    90
    Vote Rating
    0
      0  

    Default

    Thanks Animal, what could it be binded to other than 'this'? I am not familiar with using different scoping definitions...

    Thanks again

  4. #4
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Redwood City
    Posts
    30,626
    Vote Rating
    54
      0  

    Default

    If you don't need it, don't use it.

    But if you are writing "function soup" rather than a set of discrete Classes which manage their own internal state individually, then you are storing up trouble.
    Longtime Sencha geek. Outspoken advocate of pure Javascript Views. Posts my own opinions.

  5. #5
    Ext User
    Join Date
    Mar 2008
    Posts
    90
    Vote Rating
    0
      0  

    Default

    Thanks. I'm assuming by removing the scope on an event it would work across the dom of the active page? That would solve the problem I am having.

    I have removed the scrope from the listener but how can I make the event global, currently this.fireEvent.... still does not execute the listener which is without scope.

    Thanks

    John

  6. #6
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Redwood City
    Posts
    30,626
    Vote Rating
    54
      0  

    Default

    Code:
    this.grid.on('rowclick',  function(grid, index, e) {		
    		if (sm.getCount() > 0) {
    			this.fireEvent('checkactive', 'enabled');						
    		} else { 
    			this.fireEvent('checkactive', 'disabled');						
    		}
    	}, this)
    The things in red are the same. You have specified a rowclick handler to run, and passed this as the scope. So that is what the this in the handler will be.
    Longtime Sencha geek. Outspoken advocate of pure Javascript Views. Posts my own opinions.

Posting Permissions

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