1. #1
    Sencha Premium Member
    Join Date
    May 2012
    Posts
    16
    Vote Rating
    0
    mwindley is on a distinguished road

      0  

    Default Answered: Scope of 'this' problem/understanding

    Answered: Scope of 'this' problem/understanding


    I am incorrectly using 'this' and getting an error.

    Todos.js:31TypeError: 'undefined' is not a function (evaluating 'this.setUser(user)')

    My small project is to use sencha.io for user messages to improve my understanding and practices
    . I am following the recent blog example of photo sharing by Nils Dehl.

    From my init function on my controller i have a onlogin function were i want to store my user object.

    The command
    this.setUser(user) fails

    /**
    * When the controller is created it needs to
    * listen for events generated by Ext.io.Controller.
    */
    init: function() {
    this.getApplication().sio.on({
    authorized: this.onLogin,
    usermessage: this.onUserMessage,
    scope: this
    });
    },


    /**
    * When the application has an authentcated user.
    */
    onLogin: function(user) {
    console.log("onAuth", user,this)
    this.setUser(user);
    return true;
    },

    I have used console.log to inspect the instance of 'this' in my example and Nils.
    His code has created the property of 'user' and in other cases he stores channel info as well.

    I am puzzled to what I am misunderstanding and not achieving the same result.

    While not yet familiar/proficient with sencha touch by expectation was that the use of 'this.setUser()' would create the 'parameter' user in the object 'this' for me to set and get.

    Thanks in advance Maurice
    Regards Maurice

  2. Hi Maurice,

    I think you are refering to Nils modules/Share/contoller/Share.js class. He has a config block that looks like this:

    Code:
    config: {…user: null,    channel: null},
    
    The Class system will create get and set methods for every property defined in the config block. If you have a user property in the config block of your class then you should have a setUser method to call.

    You could also hold onto the user in your class by doing this.user = user which would allow you to access it from anywhere inside the instance without a getter and setter.

    Alternatively and perhaps preferably you can call Ext.io.User.getCurrent as Nils does in his example each time you need to do something with the current user:


    Code:
    Ext.io.User.getCurrent( 
        function(user, error) { 
            if (user.getId() === senderUserId) { 
                Ext.getStore('photos').sync(); 
           } 
    },this);
    
    If there isn't a user logged in then the user param will be null/undefined and error will contain an object that tells you what happened.

    Note 'this' being passed as the second parameter to getCurrent. That will allow your function to execute in the scope of your current object.

    You can read more on the user methods in our documentation:

    http://docs.sencha.io/0.3.3/index.ht...hod-getCurrent

  3. #2
    Sencha - IO Dev Team
    Join Date
    Apr 2011
    Posts
    85
    Answers
    27
    Vote Rating
    5
    jason.cline is on a distinguished road

      0  

    Default


    Hi Maurice,

    I think you are refering to Nils modules/Share/contoller/Share.js class. He has a config block that looks like this:

    Code:
    config: {…user: null,    channel: null},
    
    The Class system will create get and set methods for every property defined in the config block. If you have a user property in the config block of your class then you should have a setUser method to call.

    You could also hold onto the user in your class by doing this.user = user which would allow you to access it from anywhere inside the instance without a getter and setter.

    Alternatively and perhaps preferably you can call Ext.io.User.getCurrent as Nils does in his example each time you need to do something with the current user:


    Code:
    Ext.io.User.getCurrent( 
        function(user, error) { 
            if (user.getId() === senderUserId) { 
                Ext.getStore('photos').sync(); 
           } 
    },this);
    
    If there isn't a user logged in then the user param will be null/undefined and error will contain an object that tells you what happened.

    Note 'this' being passed as the second parameter to getCurrent. That will allow your function to execute in the scope of your current object.

    You can read more on the user methods in our documentation:

    http://docs.sencha.io/0.3.3/index.ht...hod-getCurrent

  4. #3
    Sencha Premium Member
    Join Date
    May 2012
    Posts
    16
    Vote Rating
    0
    mwindley is on a distinguished road

      0  

    Default


    Thanks for the explaination, I wil take a minute to digest it and practice it.
    Regards maurice
    Regards Maurice

Thread Participants: 1