Removing onFrame event handler

942 views
Skip to first unread message

rob

unread,
Jan 19, 2012, 6:57:53 PM1/19/12
to pap...@googlegroups.com
Hi all,

I am using paper via javascript directly and am subscribing to onFrame events by something like:

paper.view.onFrame = this.onFrame

Could somebody please tell me what is the correct way to unsubscribe the handler?   I have tried:

paper.view.onFrame = null

But this leads to some unpredictable results when I have multiple listeners subscribed to onFrame events.  

Rob 

Jonathan Puckey

unread,
Jan 26, 2012, 8:12:31 AM1/26/12
to pap...@googlegroups.com
Hi Rob,

Could you post some code that shows these unpredictable results? I have tried the code in your email, but it seems to work fine…

greetings,
Jonathan

rob

unread,
Jan 26, 2012, 7:10:09 PM1/26/12
to pap...@googlegroups.com
Sure

So I have something like this defined:

    function Test(id) {
      this.id = id;
      that = this
      paper.view.onFrame = that.onFrame;
    }

    Test.prototype.stop = function() {
      paper.view.onFrame = null;
    };

    Test.prototype.onFrame = function() {
      console.log("On Frame: " + this.id);
    };

And then I create a couple of instances and invoke stop() on both:

  var test1, test2;
  test1 = new Test('test1');
  test2 = new Test('test2');
  test1.stop();
  test2.stop();

My expectation would be that I see no console output, because I have unsubscribed both listeners before they even start, but what I get out is:

On Frame: test1
On Frame: test1
...
...
...

So it seems only the last subscription is removed. Which kind of makes sense because I can't see how paper would know which of the subscriptions I wanted to cancel.

Regards
Rob


Jonathan Puckey

unread,
Jan 27, 2012, 7:22:35 AM1/27/12
to pap...@googlegroups.com
To accomplish this, you could do the following:

function Test(id) {
this.id = id;
var that = this;
this.onFrame = function() {
// since the this object refers to
// the view here, we need to use the
// 'that' variable defined above:
console.log(that.id);
};
paper.view.attach('frame', this.onFrame);
}

Test.prototype.stop = function() {
paper.view.detach('frame', this.onFrame);
};

var test1 = new Test('test1');
var test2 = new Test('test2');
test1.stop();
test2.stop();

But for some reason I still get 'test1' in the console once.. which might be a bug..

Sure

So I have something like this defined:

    function Test(id) {
      this.id = id;
      that = this
      paper.view.onFrame = that.onFrame;
    }

    Test.prototype.stop = function() {
      paper.view.onFrame = null;
    };

    Test.prototype.onFrame = function() {
      console.log("On Frame: " + this.id);
    };

And then I create a couple of instances and invoke stop() on both:

  var test1, test2;
  test1 = new Test('test1');
  test2 = new Test('test2');
  test1.stop();
  test2.stop();

My expectation would be that I see no console output, because I have unsubscribed both listeners before they even start, but what I get out is:
On Frame: test1

rob

unread,
Jan 29, 2012, 11:26:19 PM1/29/12
to pap...@googlegroups.com
Great thanks.

Did not know about the 'attach' and 'detach' functions on the view as they are not in the reference documentation.

Regards
Rob


Reply all
Reply to author
Forward
0 new messages