Bug 16121

Summary: Web Inspector needs helper functions that pass a 'this' object to addEventListener and setTimeout
Product: WebKit Reporter: Timothy Hatcher <timothy>
Component: Web Inspector (Deprecated)Assignee: Timothy Hatcher <timothy>
Severity: Enhancement    
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Description Flags
Revised Patch aroben: review+

Description Timothy Hatcher 2007-11-24 12:55:26 PST
The Web Inspector code is very object oriented and it needs to add event listeners and timeouts that call object functions with a correct 'this' object.

A common practice sprinkles throughout the code is:

var _self = this;
setTimeout(function() { _self.foo() }, 0);
node.addEventListener('bar', function() { _self.bar() }, false);

We should add helper functions that simplify this.
Comment 1 Timothy Hatcher 2007-11-24 13:04:59 PST
Created attachment 17487 [details]
Comment 2 Adam Roben (:aroben) 2007-11-24 13:18:24 PST
Comment on attachment 17487 [details]

I was thinking about fixing this as well, but in a slightly different way. I was going to add a new function:

Function.bind = function(func, thisObject) {
    return function() { func.call(thisObject) }

Then you could do things like:

setTimeout(Function.bind(this.updateTitle, this), 0);

What do you think about that solution? I like that it's very general and so can be used in more places.
Comment 3 Adam Roben (:aroben) 2007-11-24 13:19:24 PST
(In reply to comment #2)
> Function.bind = function(func, thisObject) {
>     return function() { func.call(thisObject) }
> }

You'd want to pass arguments through, as well.
Comment 4 Timothy Hatcher 2007-11-24 13:33:12 PST
Comment on attachment 17487 [details]

I am going to redo this based on Adam's comment.
Comment 5 Timothy Hatcher 2007-11-24 14:27:21 PST
Created attachment 17488 [details]
Revised Patch
Comment 6 Adam Roben (:aroben) 2007-11-24 14:34:19 PST
Comment on attachment 17488 [details]
Revised Patch

I think "toggleExpanded" would be clearer than just "toggle" for the two places you added that function.

I don't think the order of the <script> elements matters, since none of this code gets executed as it's parsed.

Comment 7 Timothy Hatcher 2007-11-24 14:36:20 PST
Landed in r28002. http://trac.webkit.org/projects/webkit/changeset/28002