0

I am having some trouble working with oo javascript. I am creating a new object public and private methods. Now when I am calling the public method, it cannot access the private variable.

I believe I should return the public methods ( the internet ) but dont really know why. Can someone please explain what I am missing?

function something(){
    var somePanel;  // it is a jquery object to a div

    var createWindow = function(data){
        random.xhr('/chat', 'GET', null, function(res){
            var Container = $("#Container");
            somePanel = $("<div/>").addClass('somePanel').append(res);
            Container.append(somePanel.hide());
        });     
    };

    this.activate = function(){
        somePanel.show().siblings().hide();
    };

    this.init = function(data, fn){
        createWindow(data);
    };
};


connections[data] = new something();  // creates a new something object
connections[data].init(data);   //  which creates just a div object, actually
connections[data].activate();  // has code to show this panel and hide every other panel

When I call the activate() method, it cannot find somePanel. What should I be doing differently? And why?

8
  • 1
    how do you define somePanel and where? try to explain it a bit more Commented Oct 26, 2013 at 17:42
  • 1
    this code runs perfectly good; you are not showing us the not working part. But I guess you used var somewhere hiding your original somePanel from something Commented Oct 26, 2013 at 17:43
  • Also, what is the unused "that" argument for in the activate method? Also, somePanel isn't defined, so I'm guessing you're seeing Uncaught TypeError exceptions due to the value being undefined, no? Commented Oct 26, 2013 at 17:45
  • 1
    updated with some comments of the code Commented Oct 26, 2013 at 18:02
  • 1
    possible duplicate of How to return the response from an AJAX call? Commented Oct 26, 2013 at 18:22

2 Answers 2

3

Classic problem with asynchronous methods.

You're xhr handler will not have been invoked when you run activate. You need to wait for your xhr handler to run, and then, in a callback, invoke activate.

Something along these lines:

// ...

this.init = function(data, fn){
    createWindow(data, fn);
};

var createWindow = function(data, fn) {
    random.xhr('/chat', 'GET', null, function(res){
        var Container = $("#Container");
        somePanel = $("<div/>").addClass('somePanel').append(res);
        Container.append(somePanel.hide());
        fn();
    });     
};

// ...

connections[data] = new something();
connections[data].init(data, function () { connections[data].activate() });
Sign up to request clarification or add additional context in comments.

2 Comments

so my oo skills aren't to blame. glad :)
yes. i had created a callback function from the start for a different reason, however. came in handy. thanks a lot.
0

you're setting somePanel in the callback to an async call. You can't be guaranteed somePanel is set by the time you call activate().

edit meagar's answer is the best option from an OO perspective. The rest of my answer is aimed at maintaining the readability you're trying to achieve.

You may want to look into using a module that allows for asynchronous chaining (like async) or promises

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.