[mochikit] Re: callLater and this

2007-12-20 Thread Bob Ippolito

On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote:
 Is it intentional that the callLater() function causes the 'this' reference
 to get scrapped?

Has nothing to do with MochiKit, JavaScript doesn't have bound methods
like Python does. The binding of this is done by the method call
syntax. It would be impossible to preserve it unless the function was
wrapped.

See MochiKit.Base.{bind,method,bindMethods} for the typical workarounds, also:
http://bob.pythonmac.org/archives/2005/07/06/this-sucks-in-javascript/

-bob

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and this

2007-12-20 Thread Bob Ippolito

On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote:
 Thanks bob, I guess it's been about 8 years. Good to talk to you again.

High school feels a lot longer ago than that :)

 Tricky. I suppose you would run into this pretty much any time you pass
 around foo.bar when bar bound (or not) to the object foo as you wouldn't be
 able to do the call with this syntax. If your blog post is right then it
 looks like the only way to do call with this explicitly calling the
 function via the object in code.

Welcome to JavaScript.

That's pretty much what bind does, but Function.prototype.call and
Function.prototype.apply let you jigger this for a given call.

foo.bar(a, b) is the same as foo.bar.apply(foo, [a, b]) or
foo.bar.call(foo, a, b).

 Anyway, changing the test code to

  test.init = function() {
createLoggingPane(true);
callLater(3.0, bind(test.myfunc, test));
  }

 works. It's cute how bind returns a function that calls your function but
 goes out and finds the this for you first. Pretty useful hack.

Well you give it 'this' as the second argument, it doesn't do all that
much magic.

bind(test.myfunc, test) is equivalent to function () { return
test.myfunc.apply(test, arguments); }

-bob

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and this

2007-12-20 Thread Ian Lewis
Thanks bob, I guess it's been about 8 years. Good to talk to you again.

Tricky. I suppose you would run into this pretty much any time you pass
around foo.bar when bar bound (or not) to the object foo as you wouldn't be
able to do the call with this syntax. If your blog post is right then it
looks like the only way to do call with this explicitly calling the
function via the object in code.

Anyway, changing the test code to

test.init = function() {
  createLoggingPane(true);
  callLater(3.0, bind(test.myfunc, test));
}

works. It's cute how bind returns a function that calls your function but
goes out and finds the this for you first. Pretty useful hack.

Ian


2007/12/21, Bob Ippolito [EMAIL PROTECTED]:

 On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote:
  Is it intentional that the callLater() function causes the 'this'
 reference
  to get scrapped?

 Has nothing to do with MochiKit, JavaScript doesn't have bound methods
 like Python does. The binding of this is done by the method call
 syntax. It would be impossible to preserve it unless the function was
 wrapped.

 See MochiKit.Base.{bind,method,bindMethods} for the typical workarounds,
 also:
 http://bob.pythonmac.org/archives/2005/07/06/this-sucks-in-javascript/

 -bob




-- 
Ian Lewis
[EMAIL PROTECTED]
http://www.ianlewis.org

-- 
ルイス イアン
ソートス株式会社
〒101-0047
東京都千代田区内神田1丁目3番1号高砂ビル9階
TEL   : 03-5577-0800
FAX   : 03-5259-0880
E-Mail: [EMAIL PROTECTED]
URL   : http://www.thought-s.co.jp

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and this

2007-12-20 Thread Ian Lewis
2007/12/21, Bob Ippolito [EMAIL PROTECTED]:

 On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote:
  Thanks bob, I guess it's been about 8 years. Good to talk to you again.

 High school feels a lot longer ago than that :)


Indeed.

Welcome to JavaScript.


I feel a my eyes twitching when I go to sleep.

That's pretty much what bind does, but Function.prototype.call and
 Function.prototype.apply let you jigger this for a given call.

foo.bar(a, b) is the same as foo.bar.apply(foo, [a, b]) or
 foo.bar.call(foo, a, b).


Thank goodness it's there.

 works. It's cute how bind returns a function that calls your function but
  goes out and finds the this for you first. Pretty useful hack.

 Well you give it 'this' as the second argument, it doesn't do all that
 much magic.


Only if you're kind enough to give it. I just happen to be a nice guy.

Anywho, thanks for the insight.

-- 
Ian Lewis
[EMAIL PROTECTED]
http://www.ianlewis.org

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and IE

2007-08-22 Thread Jonathan Marshall

All ready, however when I try to submit a ticket I am told:
Internal Error: Submission rejected as potential spam (Akismet says
content is spam)

The patch is:

Index: tests/test_Base.js
===
--- tests/test_Base.js  (revision 1312)
+++ tests/test_Base.js  (working copy)
@@ -26,6 +26,9 @@
 t.is( bind(boundFunc, undefined, foo)(), self foo, boundFunc
partial no self change );
 t.is( bind(boundFunc, not_self, foo)(), not self foo,
boundFunc partial self change );

+t.is( typeof(bind(alert).im_func.apply), function, true, bind
alert worked );
+t.is( typeof(bind(window.print).im_func.apply), function, bind
window.print worked );
+
 // test method
 not_self = {toString: function () { return not self; } };
 self = {toString: function () { return self; } };
Index: MochiKit/Base.js
===
--- MochiKit/Base.js(revision 1312)
+++ MochiKit/Base.js(working copy)
@@ -607,7 +607,7 @@
 var im_preargs = func.im_preargs;
 var im_self = func.im_self;
 var m = MochiKit.Base;
-if (typeof(func) == function  typeof(func.apply) ==
undefined) {
+if (typeof(func) == object  typeof(func.apply) ==
undefined) {
 // this is for cases where JavaScript sucks ass and gives
you a
 // really dumb built-in function like alert() that
doesn't have
 // an apply

I'm not 100% happy with the test patch however as it tests internal
behaviour of bind rather than testing binds' interface, however I
don't know how to non-intrusively test that bind will work with alert
 window.print.



On Aug 21, 5:00 pm, Arnar Birgisson [EMAIL PROTECTED] wrote:
 On 8/21/07, Jonathan Marshall [EMAIL PROTECTED] wrote:

  Changing that to:
  if (typeof(func) == object  typeof( func.apply) == undefined) {
  works for me.

 Good. Would you be able to add some tests using your examples and
 submit them as well as the fix as a patch? Then we can test it on the
 various platforms and commit it if everything works.

 Arnar


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and IE

2007-08-21 Thread Bob Ippolito

On 8/21/07, Jonathan Marshall [EMAIL PROTECTED] wrote:

 Hi,

 I'm not sure if this is a bug in Mochikit, or my error.

 The following do not work in IE 6:

 script
 callLater(1, window.print);
 callLater(2, alert, 'drink beer');
 /script

 yet these do:
 script
 function f() {
 window.print();
 }
 function g() {
 alert('drink beer');
 }
 callLater(1, f);
 callLater(2, g);
 /script

 Works beautifully in Firefox though.Tried with Mochikit 1.3.1 and a
 recent dump from SVN.

Some of these built-in methods and functions aren't actual JavaScript
functions and aren't compatible with Function.prototype.apply, so
using MochiKit directly on them may not work. I think there's a
workaround for it in MochiKIt that makes it work in Firefox and maybe
Safari but I guess that workaround doesn't work properly in IE6.

-bob

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and IE

2007-08-21 Thread Arnar Birgisson

On 8/21/07, Bob Ippolito [EMAIL PROTECTED] wrote:
 Some of these built-in methods and functions aren't actual JavaScript
 functions and aren't compatible with Function.prototype.apply, so
 using MochiKit directly on them may not work. I think there's a
 workaround for it in MochiKIt that makes it work in Firefox and maybe
 Safari but I guess that workaround doesn't work properly in IE6.

They are proper functions in Firefox and I believe also in Safari.
This is a known problem in IE which doesn't expose the proper Function
interface on it's built-ins.

There *is* a workaround in MochiKit, applied in Base.bind by wrapping
the function in Base._wrapDumbFunction. Maybe the condition that is
used to check if that workaround should be applied needs revision.
Currently it's
if (typeof(func) == function  typeof(func.apply) == undefined) {

I don't have time atm or the proper debugging tools to test this in
IE, but if there are no volunteers I'll have a look sometime.

Arnar

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and IE

2007-08-21 Thread Jonathan Marshall
On 8/21/07, Arnar Birgisson [EMAIL PROTECTED] wrote:


 There *is* a workaround in MochiKit, applied in Base.bind by wrapping
 the function in Base._wrapDumbFunction. Maybe the condition that is
 used to check if that workaround should be applied needs revision.
 Currently it's
 if (typeof(func) == function  typeof(func.apply) == undefined) {

 I don't have time atm or the proper debugging tools to test this in
 IE, but if there are no volunteers I'll have a look sometime.

 Arnar



Changing that to:
if (typeof(func) == object  typeof( func.apply) == undefined) {

works for me.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: callLater and IE

2007-08-21 Thread Arnar Birgisson

On 8/21/07, Jonathan Marshall [EMAIL PROTECTED] wrote:
 Changing that to:
 if (typeof(func) == object  typeof( func.apply) == undefined) {
 works for me.

Good. Would you be able to add some tests using your examples and
submit them as well as the fix as a patch? Then we can test it on the
various platforms and commit it if everything works.

Arnar

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---