Reputation: 2228
Could someone explain or show me how to incorporate two JavaScript objects, maybe I can call them widgets, to work then together.
Lets say I have a big object (window) which represents a explorer window and a smaller object (toolbar) which represent toolbar with various buttons. Manipulating them alone (add, delete, change properties and etc.) I have no problems. But how do I make them work together?
For example, if I want to remove toolbar from window in this way
window.Toolbar.Remove()
Remove()
function is part of toolbar
object and would remove toolbar it self from DOM, but how do I remove it's reference from window object and set it to null
. Should I destroy this toolbar
some how?
And if I destroy main parent object window
, how should I destroy all smaller objects like toolbar
?
Any pointers highly appreciated.
Upvotes: 0
Views: 104
Reputation: 16182
I see two possible options here:
1) Make the child widget aware of parent.
You can have something like this (pseudo-code):
window.addChild(name, WigdetClass) {
this[name] = new WigdetClass(this);
}
// add toolbar
window.addChild('toolbar', Toolbar);
// Toolbar constructor accepts `parent` as parameter
Toolbar(parent) {
this.parent = parent;
}
Toolbar.prototype.Remove() {
// remove self from the page
...
// set parent reference to null
parent.toolbar = null;
}
2) Or you can move "Remove" to the parent object:
Window.prototype.RemoveChild(child) {
// remove child from the document
...
var propertyName = this.findChildName(child); // 'toolbar'
this[propertyName] = null;
}
Upvotes: 1