Re: [jQuery] msie closures syntax

2006-12-28 Thread Jonathan Sharp

On 12/27/06, moe <[EMAIL PROTECTED]> wrote:


yep, it's for use with multiple elements,
so that global attr()-stuff doesn't cut it.

but fear not, Choan's helper function did it for me:

$(this)[0].ontimer = setTimeoutH( function( mysrc, body, x, y ) { on(
mysrc, body, x, y ) },



I think you can turn the line above into the line below since all of the
arguments are the same:
"$(this)[0].ontimer = setTimeoutH(on,"

-js
___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] msie closures syntax

2006-12-27 Thread moe
On Wed, Dec 27, 2006 at 11:25:20AM -0800, Michael Geary wrote:
> "this" doesn't work like a local variable. Inside a nested function, "this"
> is not the same as in the outer function. That's what is messing things up.
> 
> If I understand your code, you can write it more simply like this:
> 
> (function($) {
> $.fn.Tooltip = function(settings) {
> var $all = this;
> settings = $.extend($.extend({}, arguments.callee.defaults),
> settings || {});
> $all.filter('[EMAIL PROTECTED]').bind( settings.event, onmouseover );
> return this;
> 
> function onmouseover( event ) {
> //...
> setTimeout(
> function() { 
> on( $all.attr('id'), $all.attr('ttbody'),
> event.pageX + settings.xoffset,
> event.pageY + settings.yoffset
> );
> }, settings.ondelay );
> //...
> }
> 
> function on( mysrcid, body, x, y ) {
> // do stuff...
> }
> };
> })(jQuery);
> 
> But now that it's simple enough for me to understand, one question comes to
> mind: Do you use this plugin only with a single element, e.g.
> $('#foo').Tooltip(), or can it use multiple elements, e.g.
> $('.foo').Tooltip()? The two attr() calls inside the setTimeout don't look
> right for multple elements.
 
yep, it's for use with multiple elements,
so that global attr()-stuff doesn't cut it.

but fear not, Choan's helper function did it for me:

$(this)[0].ontimer = setTimeoutH( function( mysrc, body, x, y ) { on( mysrc, 
body, x, y ) },
  this.tSettings.ondelay,
  $(this).attr('id'),
  $(this).attr('ttbody'),
  event.pageX + this.tSettings.xoffset,
  event.pageY + this.tSettings.yoffset
);

yes, it's still a helper function, but at least a
generic one. :)  and this way it works in all browsers.


regards, moe

-- 
Death before dishonor.  But neither before breakfast.


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] msie closures syntax

2006-12-27 Thread Michael Geary
"this" doesn't work like a local variable. Inside a nested function, "this"
is not the same as in the outer function. That's what is messing things up.

If I understand your code, you can write it more simply like this:

(function($) {
$.fn.Tooltip = function(settings) {
var $all = this;
settings = $.extend($.extend({}, arguments.callee.defaults),
settings || {});
$all.filter('[EMAIL PROTECTED]').bind( settings.event, onmouseover );
return this;

function onmouseover( event ) {
//...
setTimeout(
function() { 
on( $all.attr('id'), $all.attr('ttbody'),
event.pageX + settings.xoffset,
event.pageY + settings.yoffset
);
}, settings.ondelay );
//...
}

function on( mysrcid, body, x, y ) {
// do stuff...
}
};
})(jQuery);

But now that it's simple enough for me to understand, one question comes to
mind: Do you use this plugin only with a single element, e.g.
$('#foo').Tooltip(), or can it use multiple elements, e.g.
$('.foo').Tooltip()? The two attr() calls inside the setTimeout don't look
right for multple elements.

-Mike

> well, i need to pass some arguments to doStuff() that seem to 
> be out of scope when it fires.
> 
> to clarify, below is the relevant snippet of my real code 
> with your variant applied:
> 
> snip
> 
> (function($) {
> $.fn.Tooltip = function(settings) {
> settings = $.extend($.extend({}, arguments.callee.defaults),
settings || {});
> 
> $(this).filter('[EMAIL PROTECTED]')
> .each(function() {
> this.tSettings = settings;
> })
> .bind( settings.event, onmouseover );
> return this;
> };
> 
> //...
> 
> function onmouseover( event ) {
> //...
>  //
>  // firebug throws "this.tSettings has no properties" on the
next line
>  // (when the timeout fires)
>  //
>  setTimeout(
> function() { 
> on( $(this).attr('id'),
> $(this).attr('ttbody'),
> event.pageX + 
> this.tSettings.xoffset,event.pageY + this.tSettings.yoffset
>   );
>  }, this.tSettings.ondelay );
> //...
> }
> 
> function on( mysrcid, body, x, y ) {
> // do stuff...
> }
> 
> //...
> 
> })(jQuery);
> 
> snap
> 
> as you may have guessed i'm tampering with the tooltip plugin 
> and am probably missing something obvious...


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] msie closures syntax

2006-12-27 Thread moe
nice, works in all browsers, solved my immediate problem.

thank you! :)

On Wed, Dec 27, 2006 at 01:09:33PM +0100, Choan C. Gálvez wrote:
> I was intrigued by the syntax used by Moe, as I had never seen it.
> 
> It's documented at
> , stating:
> 
> "Note that passing additional parameters to the function in the first
> syntax does not work in Internet Explorer."
> 
> Anyway, as I see possible uses for it, I've coded a helper:
> 
>   function setTimeoutH(f, t) {
>   var params = [].slice.call(arguments, 2);
>   var f2 = function() {
>   f.apply(null, params);
>   }
>   return window.setTimeout(f2, t);
>   }
> 
> Hope it helps someone.
 

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] msie closures syntax

2006-12-27 Thread moe
Hi Mike,

On Wed, Dec 27, 2006 at 12:52:49AM -0800, Michael Geary wrote:
> Not only not related to jQuery, but not related to closures either. :-)

hrmbl, but it was such a nice keyword... ;)

> The problem is that setTimeout doesn't accept the additional arguments you
> are passing it.
> 
> Is there any reason you can't do this:
> 
> setTimeout( function() { doStuff( "stuff", 1, 2 ); }, 100 );

well, i need to pass some arguments to doStuff() that seem to
be out of scope when it fires.

to clarify, below is the relevant snippet of my real code
with your variant applied:

snip

(function($) {
$.fn.Tooltip = function(settings) {
settings = $.extend($.extend({}, arguments.callee.defaults), settings 
|| {});

$(this).filter('[EMAIL PROTECTED]')
.each(function() {
this.tSettings = settings;
})
.bind( settings.event, onmouseover );
return this;
};

//...

function onmouseover( event ) {
//...
 //
 // firebug throws "this.tSettings has no properties" on the next 
line
 // (when the timeout fires)
 //
 setTimeout(
function() { 
on( $(this).attr('id'),
$(this).attr('ttbody'),
event.pageX + this.tSettings.xoffset,event.pageY + 
this.tSettings.yoffset
  );
 }, this.tSettings.ondelay );
//...
}

function on( mysrcid, body, x, y ) {
// do stuff...
}

//...

})(jQuery);

snap

as you may have guessed i'm tampering with the tooltip plugin
and am probably missing something obvious...


regards,
  moe


working example quoted for reference:

> > //
> > // B) this works in IE
> > //
> > var fref = iehelper( "stuff", 1, 2 );
> > setTimeout( fref, 100 );
> > 
> > function iehelper( a,b,c ) {
> > return ( function() {
> > doStuff( a,b,c );
> > });
> > }


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] msie closures syntax

2006-12-27 Thread Yehuda Katz

Another possible improvement would be to genericize the work I did on the
speed test.

Specifically, to avoid browser lockup, I did the following:
* I pass a list of DOM Elements to a function
* That function executes something on the first item, and when it's done,
passes the same list with the first item chopped off back to the same
function via setTimeout

Browsers lock up when they run JavaScript continuously. Using this method,
you can create small (few millisecond) breathing space between iterations,
which results in an avoidance of lockup or "non-responsive" errors. The only
drawback is that you need to create manual recursive methods to handle it.

I wonder if your method above could be expanded to support something like
$.runSequentially(list, function() { // some code to run sequentially });

-- Yehuda

On 12/27/06, Choan C. Gálvez <[EMAIL PROTECTED]> wrote:


On 12/27/06, Michael Geary <[EMAIL PROTECTED]> wrote:
> Not only not related to jQuery, but not related to closures either. :-)
>
> The problem is that setTimeout doesn't accept the additional arguments
you
> are passing it.
>
> Is there any reason you can't do this:
>
> setTimeout( function() { doStuff( "stuff", 1, 2 ); }, 100 );
>
> That would work in any browser.

I was intrigued by the syntax used by Moe, as I had never seen it.

It's documented at
, stating:

"Note that passing additional parameters to the function in the first
syntax does not work in Internet Explorer."

Anyway, as I see possible uses for it, I've coded a helper:

function setTimeoutH(f, t) {
var params = [].slice.call(arguments, 2);
var f2 = function() {
f.apply(null, params);
}
return window.setTimeout(f2, t);
}

Hope it helps someone.



> > i hope someone on this list can help me here, even tho
> > my question is not directly related to jquery (duck).
> >
> > i have trouble getting my closures to work in ie:
> >
> >
> > --snip
> >
> > //
> > // A) this doesn't work in ie (ff & opera grok it)
> > //
> > setTimeout(  function( a,b,c ) { doStuff( a,b,c ) }, 100,
> > "stuff", 1, 2  );
> >
> >
> > //
> > // B) this works in IE
> > //
> > var fref = iehelper( "stuff", 1, 2 );
> > setTimeout( fref, 100 );
> >
> > function iehelper( a,b,c ) {
> > return ( function() {
> > doStuff( a,b,c );
> > });
> > }
> >
> > --snap
> >
> >
> > anyone know how to feed that to ie without
> > the nasty helper function?
--
Choan


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/





--
Yehuda Katz
Web Developer | Wycats Designs
(ph)  718.877.1325
___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] msie closures syntax

2006-12-27 Thread Choan C. Gálvez
On 12/27/06, Michael Geary <[EMAIL PROTECTED]> wrote:
> Not only not related to jQuery, but not related to closures either. :-)
>
> The problem is that setTimeout doesn't accept the additional arguments you
> are passing it.
>
> Is there any reason you can't do this:
>
> setTimeout( function() { doStuff( "stuff", 1, 2 ); }, 100 );
>
> That would work in any browser.

I was intrigued by the syntax used by Moe, as I had never seen it.

It's documented at
, stating:

"Note that passing additional parameters to the function in the first
syntax does not work in Internet Explorer."

Anyway, as I see possible uses for it, I've coded a helper:

function setTimeoutH(f, t) {
var params = [].slice.call(arguments, 2);
var f2 = function() {
f.apply(null, params);
}
return window.setTimeout(f2, t);
}

Hope it helps someone.



> > i hope someone on this list can help me here, even tho
> > my question is not directly related to jquery (duck).
> >
> > i have trouble getting my closures to work in ie:
> >
> >
> > --snip
> >
> > //
> > // A) this doesn't work in ie (ff & opera grok it)
> > //
> > setTimeout(  function( a,b,c ) { doStuff( a,b,c ) }, 100,
> > "stuff", 1, 2  );
> >
> >
> > //
> > // B) this works in IE
> > //
> > var fref = iehelper( "stuff", 1, 2 );
> > setTimeout( fref, 100 );
> >
> > function iehelper( a,b,c ) {
> > return ( function() {
> > doStuff( a,b,c );
> > });
> > }
> >
> > --snap
> >
> >
> > anyone know how to feed that to ie without
> > the nasty helper function?
-- 
Choan


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] msie closures syntax

2006-12-27 Thread Michael Geary
Not only not related to jQuery, but not related to closures either. :-)

The problem is that setTimeout doesn't accept the additional arguments you
are passing it.

Is there any reason you can't do this:

setTimeout( function() { doStuff( "stuff", 1, 2 ); }, 100 );

That would work in any browser.

-Mike

> i hope someone on this list can help me here, even tho
> my question is not directly related to jquery (duck).
> 
> i have trouble getting my closures to work in ie:
> 
> 
> --snip
> 
> //
> // A) this doesn't work in ie (ff & opera grok it)
> //
> setTimeout(  function( a,b,c ) { doStuff( a,b,c ) }, 100, 
> "stuff", 1, 2  );
> 
> 
> //
> // B) this works in IE
> //
> var fref = iehelper( "stuff", 1, 2 );
> setTimeout( fref, 100 );
> 
> function iehelper( a,b,c ) {
> return ( function() {
> doStuff( a,b,c );
> });
> }
> 
> --snap
> 
> 
> anyone know how to feed that to ie without
> the nasty helper function?


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


[jQuery] msie closures syntax

2006-12-26 Thread moe
hi all :)

i hope someone on this list can help me here, even tho
my question is not directly related to jquery (duck).

i have trouble getting my closures to work in ie:


--snip

//
// A) this doesn't work in ie (ff & opera grok it)
//
setTimeout(  function( a,b,c ) { doStuff( a,b,c ) }, 100, "stuff", 1, 2  );


//
// B) this works in IE
//
var fref = iehelper( "stuff", 1, 2 );
setTimeout( fref, 100 );

function iehelper( a,b,c ) {
return ( function() {
doStuff( a,b,c );
});
}

--snap


anyone know how to feed that to ie without
the nasty helper function?


best regards,
  moe


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/