Bug 16121 - Web Inspector needs helper functions that pass a 'this' object to addEventListener and setTimeout
Summary: Web Inspector needs helper functions that pass a 'this' object to addEventLis...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (Deprecated) (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Enhancement
Assignee: Timothy Hatcher
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-24 12:55 PST by Timothy Hatcher
Modified: 2007-11-24 14:36 PST (History)
0 users

See Also:


Attachments
Patch (23.92 KB, patch)
2007-11-24 13:04 PST, Timothy Hatcher
no flags Details | Formatted Diff | Diff
Revised Patch (22.91 KB, patch)
2007-11-24 14:27 PST, Timothy Hatcher
aroben: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
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]
Patch
Comment 2 Adam Roben (:aroben) 2007-11-24 13:18:24 PST
Comment on attachment 17487 [details]
Patch

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]
Patch

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.

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