1

I have a problem with passing events from one object to another in Javascript. I tried to use PubSub to do that. Let me give an example:

function A() {

    function _publish(){
        PubSub.publish("test");
    }
    function _doSth() {
        console.log("Done!");
    }

    return {
        publish: _publish,
        doSth: _doSth
    };
}

function B() {
    var objA = new A();

    PubSub.subscribe("test", function(){
       objA.doSth(); 
    });  

}

That code example is similar to object structure in my application. Instance of B object is public, but instance of object A is private inside B object.

The problem is, that when event "test" is published, it is handled by the pubsub, but because objA is not global, I see in console that doSth function is undefined.

Anyone have an idea how to solve that?

jacobs
  • 11
  • 1
  • `but because objA is not global` this can't be an issue for pub-sub pattern. Unless.. What PubSub implementation are you using? And where do you call `objA.publish()`? – dfsq Nov 03 '14 at 13:47
  • I was using amplify.js and nunt, none of them have worked. But I tried PubSub.js now and it looks like everything works :O. – jacobs Nov 03 '14 at 13:52

1 Answers1

0

I dont know what is pubsub but objA is local inside B is not an issue you can check this code

function A() {

    function _publish(){

    }
    function _doSth() {
        console.log("Done!");
    }

    return {
        publish: _publish,
        doSth: _doSth
    };
}

function B() {
    var objA = new A();    
    objA.doSth();
}
new B();

// Sorry this is not an answer but i dont have reputation being able to comment. So i had to written in answer box

intekhab
  • 1,566
  • 1
  • 13
  • 19