0

I have an array of lightweight objects, each of which is the Subject of an Observer pattern. To conserve memory, when the Subject is no longer observed, i want to release the object's resources and make it remove itself from the parent array. So how do I ask either the parent object or the Array itself to splice the item, from code within the item itself? What I came up with is something like:

var parentObj = {
   items : [],
   addItem : function () {
       var newItem = new ItemObj;
       items.push(newItem);
   },
   removeItem : function (item) {
       for (var i = this.items.length; i--; i < 0) {
            if (this.items[i] == item) {
                this.items.splice(i, 1);
                return;
            }
        }
    }
};

function ItemObj() {}
ItemObj.prototype = {
   observers : [],
   observerRemove : function(observer){
       //find observer in observers array and splice it out
           ....
       //now here's the part where it gets tricky
       if (this.observers.length == 0) {
          parentObj.removeItem(this);
       }
   },
   //observerAdd.... etc
 };

Which works, but only because parentObj is a named variable, if it were a class, it would not be so easy. Also, this seems a little clumsy. It would be nice if ItemObj could have some reference to it's parent Array object, but I can't find that. Any suggestions? Perhaps passing a reference from parentObj of itself to each ItemObj? as in

              newItem.parentArray = this.items;

when creating the itemObj? Again, seems clumsy.

1 Answer 1

2

Why not just add a reference to the parent in the item class.

var parentObj = {
   items : [],
   addItem : function () {
       var newItem = new ItemObj;
       newItem.parent = this; // set the parent here
       items.push(newItem);
   },
   removeItem : function (item) {
       for (var i = this.items.length; i--; i < 0) {
            if (this.items[i] == item) {
                this.items.splice(i, 1);
                return;
            }
        }
    }
};

function ItemObj() {}
ItemObj.prototype = {
   parent: null,
   observers : [],
   observerRemove : function(observer){
       //find observer in observers array and splice it out
           ....
       //now here's the part where it gets tricky
       if (this.observers.length == 0) {
          this.parent.removeItem(this); // use the parent here
       }
   },
   //observerAdd.... etc
 };
Sign up to request clarification or add additional context in comments.

2 Comments

Should it be this.parent.removeItem(this);?
Ah, that seems so simple now that I look at it. Thanks guys.

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.