Results 1 to 8 of 8

Thread: How do you call a function in a controller from within its self.

  1. #1

    Default Answered: How do you call a function in a controller from within its self.

    Im working on events and trying to get a grasp on loosely coupling things in sencha, and i've created a central dispatcher which is basically the application itself.

    I'm confused on the key words "this", and "self", and "this.self", that seem to be different at certain points in time, and I was wondering if there was any documentation links that anyone knew of that could help me out with understanding it.

    Now to the main question. I have a class NetworkController, that will monitor my network ( yes i know there are base phone gap events etc, but this is just an example )

    Basically whenever I call the
    Code:
    this.urlMonitorId = this.self.callTimer();
    I get a runtime error saying that 'callTimer" doesnt exist. I've also tried a variation of just calling callTimer(), and this.callTimer()

    Uncaught TypeError: Object function (){return this.constructor.apply(this,arguments)} has no method 'callTimer'

    Im just confused as to how i get a reference to my own class's reference within the handler, I see examples of how to create a User class that extends Person, then they do Ext.create(.... then call the speak() method, but I'm just not seeing how to call methods inside my own class that I want the class to use internally?

    Code:
    Ext.define('app.controller.NetworkController',{
        extend: 'app.controller.BaseController',
        
        mixins: {
            observable: 'Ext.util.Observable'
        },
        
        launch: function(){
            this.callParent();
            
            this.interval = 2000;
            
            this.dispatcher.on({
                SETUP_URL_MONITORING: this.handleSetupUrlMonitoring
            })
        },
        
        handleSetupUrlMonitoring: function( application ){
            console.log('[' + this.getName() + '.SETUP_URL_MONITORING()]' + application );
            
            console.debug('[' + this.getName() + '.SETUP_URL_MONITORING()]' );
            
            //this.urlMonitorId = this.self.callTimer();
            debugger
        }, 
        
        
        callTimer: function()
        {
            // Call some sort of ajax proxy that will ping my server and tell me if it responds.
            return setTimeout( handleCallTimer, this.interval );
        },
        
        handleCallTimer: function()
        {
            console.log('[' + this.self.getName() + '.handleCallTimer()]' );
            
            callTimer();
        }
    })

  2. Try adding scope : this in the on() of the dispatcher call.

  3. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,448
    Answers
    3997

    Default

    You should just be able to do

    Code:
    this.callTimer();
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  4. #3

    Default no go on this.callTimer()

    Yea, definitely already tried that.

    Code:
    console.debug('[this.callTimer]' + this.callTimer); // undefined
    console.debug('[this.self.callTimer]' + this.self.callTimer); // undefined
    console.debug('[callTimer]' + callTimer); // results: RTE (Uncaught ReferenceError: callTimer is not defined)

  5. #4
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,448
    Answers
    3997

    Default

    Is this the function you pass in the setTimeout? If so then that's because the scope is wrong.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  6. #5

    Default naw

    Naw, this is the function Im calling after my event is fired from a different controller..


    I basically have

    ApplicationController.launch > myDispatcher.fireEvent( 'NetworkEventSETUP_URL_MONITORING' );

    then NetworkController:

    Code:
    launch: function(){
            this.callParent();
            
            this.interval = 2000;
            
            this.dispatcher.on({
                NetworkEventSETUP_URL_MONITORING: this.handleSetupUrlMonitoring
            })
    },
    
    handleSetupUrlMonitoring: function( application ){
            console.log('[' + this.getName() + '.SETUP_URL_MONITORING()]' + application );
            
            console.debug('[' + this.getName() + '.SETUP_URL_MONITORING()]' );
            
            //this.urlMonitorId = this.self.callTimer();
            console.debug('[this.callTimer]' + this.callTimer); // undefined
            console.debug('[this.self.callTimer]' + this.self.callTimer); // undefined
            console.debug('[callTimer]' + callTimer); // results: RTE (Uncaught ReferenceError: callTimer is not defined)
            debugger
            //this.urlMonitorId = this.callTimer();
    },

  7. #6

    Default launch

    in the launch() function I do actually get responses when i log it.

    Code:
    launch: function(){
            this.callParent();
            
            this.interval = 2000;
            
            this.dispatcher.on({
                NetworkEventSETUP_URL_MONITORING: this.handleSetupUrlMonitoring
            });
            
            console.debug('[this.callTimer]' + this.callTimer); 
        },
    //log    [this.callTimer]function ()    {        
    // Call some sort of ajax proxy that will ping my server and tell me if it responds.        
    return setTimeout( handleCallTimer, this.interval );    
    }
    I'm just confused as to why the "this" scope works in the launch function, but then in my handler "handleSetupUrlMonitoring", it seems to be different?

  8. #7
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,448
    Answers
    3997

    Default

    Try adding scope : this in the on() of the dispatcher call.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  9. #8

    Default That worked

    That worked, all though by doing that I could no longer really do what i wanted to do in that function, by firing other events and such, but regardless, that was a really good thing to know.

    Thank you.

Tags for this Thread

Posting Permissions

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