Results 1 to 3 of 3

Thread: Scope of 'this' problem/understanding

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Sencha Premium Member
    Join Date
    May 2012
    Vote Rating

    Default 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 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
    init: function() {
    authorized: this.onLogin,
    usermessage: this.onUserMessage,
    scope: this

    * When the application has an authentcated user.
    onLogin: function(user) {
    console.log("onAuth", user,this)
    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:

    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 as Nils does in his example each time you need to do something with the current user:

        function(user, error) { 
            if (user.getId() === senderUserId) { 
    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:

Posting Permissions

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