0

The wording of the title probably needs re-visiting, but for now consider the following code:

var ParentControl = function()
{
    this.ShowAlert = function()
    {
        alert('hi!');
    }

    this.childControl = new ChildControl();
};

var ChildControl = function()
{
    // IN THIS SCOPE HERE I NEED TO CALL 'ShowAlert'
};

var parentControl = new ParentControl();

As per the comment in the code snippet above, I need instances of ChildControl to call the 'ShowAlert' function of their parent but I don't know how.

What I've Tried

Simply calling ParentControl.ShowAlert() where I've wrote the comment - obviously doesn't work as parentControl is the instance (I don't want to refer to this explicitly).

I've also tried passing in the parent control to the constructor of ChildControl but I ideally wanted to keep the child control independent from any particular type of parent.

  • 1
    *"I ideally wanted to keep the child control independent from any particular type of parent"* In that case, how about some kind of event publisher/subscribe pattern? The `ChildControl` instance emits an event/message and the `ParentControl` instance reacts to it. – Felix Kling Jun 10 '14 at 15:20
  • @FelixKling - this sounds ideal. I know I could expose this event as `this.somethingHappened` on the child but how could the parent know that something happened? Or is my approach wrong? –  Jun 10 '14 at 15:21
  • I'm not very good at reading, and still don't understand what the issue with just passing `new ChildControl(this);` inside the parent is ? – adeneo Jun 10 '14 at 15:22
  • or this -> http://jsfiddle.net/mQMUc/ ? – adeneo Jun 10 '14 at 15:25
  • 1
    Inside the child, you do `this.emit('foo')` and the parent has to listen to the child with `this.childControl.subscribe('foo', callback)`. In your case it seems that the event would be emitted before the parent subscribes, in which case you also have to implement a mechanism that "replays" emitted events to new subscribers. **However** passing the parent as dependency to the child is a much simpler solution, and you should go with that if there are no major reasons against it. Keep it simple ;) – Felix Kling Jun 10 '14 at 15:26

1 Answers1

1

I'd also move showAlert onto the prototype, so you're not creating a new function in memory every time you instantiate ParentControl.

var ParentControl = function() {
    this.childControl = new ChildControl(this);
};

ParentControl.prototype.showAlert = function() {
  console.log('hello');
};

var ChildControl = function(parent) {
  parent.showAlert();
};

var parentControl = new ParentControl();
Alexis Abril
  • 6,389
  • 6
  • 33
  • 53
  • So, the OP is saying *"I've also tried passing in the parent control to the constructor of ChildControl but, I ideally wanted to keep the child control independent from any particular type of parent."*, and your answer is *"just pass the parent to the child"*? It seems like you focused on a technicality (the prototype) instead of the actual question. – Felix Kling Jun 10 '14 at 15:27