Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:51:25+00:00 2026-06-06T18:51:25+00:00

This could be a very basic Javascript I know but I just can’t get

  • 0

This could be a very basic Javascript I know but I just can’t get it..

Ext.regController('Chat', {
    initSocketConnection: function() {
    this.chatStore = new App.Store.Chat();
            ...
    this.socket = io.connect(settings.get('server'), {port: 8080});

    this.socket.on(
        'message',
        this.addMessageToChatStore
    );

},

addMessageToChatStore: function(message) {
    console.log(message);
        console.log(this);     << console shows the 'this' has become SocketNameSpace
    this.chatStore.add(message);   << this line error with "Undefined" chatStore

    this.send(message);
},

Console print out shows that the "this" in the addMessageToChatStore function is "SocketNamespace"

How to I get rid of the error?

To generalize the problem. I think it’s better to describe it as function chain calling dilemma.

A class has some local var that’s instance of another class. When this var listens on certain events, it calls the parent’s class’s method. The problem is when this method is called, it’s under the context of the other class and hence the scope has changed and the access to the original parent’s class methods are denied.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:51:28+00:00Added an answer on June 6, 2026 at 6:51 pm

    In javascript, the this variable is determined by the caller. You could use a self executing function to ensure you’re referencing the correct entity rather than using this:

    Ext.regController('Chat', (function() {
      var self = {
        initSocketConnection: function() {
          self.chatStore = new App.Store.Chat();
          ...
          self.socket = io.connect(settings.get('server'), {port: 8080});
    
          self.socket.on(
            'message',
            this.addMessageToChatStore
          );
        },
        addMessageToChatStore: function(message) {
          console.log(message);
          self.chatStore.add(message);
          self.send(message);
        }
      };
      return self;
    }()));
    

    Update

    Based on the full snippet, I’m not sure where the render function is defined, if render is a global function then self.viewChat = render({xtype : 'App.View.Chat'}); will suffice, otherwise it may be defined on this (the this that is defined for the call to index) so the following may suffice self.viewChat = this.render({xtype : 'App.View.Chat'});.

    If I were a betting man, I’d go for the latter, so the code would be (with commented out code removed):

    Ext.regController('Chat', (function() {
        var self = {
            index: function() {
                if (!self.socket) {
                    self.initSocketConnection();
                }
    
                self.showChat();
            },
            /**
             * init the socket connection to the node.js server
             * using user settings
             *
             */
            initSocketConnection: function() {
                self.chatStore = new App.Store.Chat();
                self.chatStore.add({
                    user: "Figo",
                    message: "Welcome!"
                });
                self.configStore = Ext.StoreMgr.get('ConfigStore');
                var settings = self.configStore.getAt(0);
    
                self.socket = io.connect(settings.get('server'), {
                    port: 8080
                });
    
                // Event Listener
                self.socket.on('connect', self.registerUser);
                self.socket.on('message', self.addMessageToChatStore);
                App.on('newMsg', self.sendMessageToServer);
            },
            sendMessageToServer: function(msg) {
                self.socket.send(msg);
            },
            addMessageToChatStore: function(message) {
                console.log(message);
                console.log(this);
                console.log(this.parent);
    
                self.chatStore.add(message);
                self.socket.send(message);
            },
            registerUser: function() {
                self.configStore = Ext.StoreMgr.get('ConfigStore');
                var settings = self.configStore.getAt(0);
    
                var user = {
                    nickname: settings.get('nickname'),
                    gravatar: settings.get('gravatar')
                };
                console.log(user);
                self.socket.send(user);
            },
            /**
             * Show chat view
             */
            showChat: function() {
                if (!self.viewChat) {
    
                    self.viewChat = this.render({
                        xtype: 'App.View.Chat'
                    });
    
                    self.viewChat.query('#settingsButton')[0].on('tap', self.showConfig, self);
                }
                self.application.viewport.setActiveItem(
                self.viewChat, {
                    type: 'slide',
                    direction: 'left'
                });
            },
            /**
             * Show config View
             */
            showConfig: function() {
                Ext.dispatch({
                    controller: 'Viewport',
                    action: 'showConfig'
                });
            }
        };
        return self;
    }()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a very basic problem, but I sort of don't know SQL
This probably is a very very basic question but i can't seem to find
This could very well be a basic question - but I was unable to
This could be a very basic question for prodigies.But I have a doubt to
This could be a very basic question, but hopefully someone will be able to
This could be a very basic question but i want to confirm here. Im
I know this question is probably very basic, but I am a beginner so
I know this is very basic but i am frustrated like anything.. i googled
I think this could be a very easy question for you. But I have
Introduction: Now I know this question could be very broad and it would be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.