[Proto-Scripty] Re: Calling function passed as parameter
Hi, What's the error? -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Feb 17, 10:53 am, marioosh marioosh@gmail.com wrote: I have a problem with calling function passed as parameter in class definition. I get error when i call fun(). How can i correct this ? var Slider = Class.create({ initialize: function (elContainer) { this.elContainer = elContainer; if($(this.elContainer).getStyle('left') == '0px') { this.minimize(this.doit); } else { this.doit(); } }, minimize: function(fun) { new Effect.Morph(this.elContainer, { style: 'left: 940px;', duration: 0.3, afterFinish: function() { $(this.elContainer).setStyle({ left: '-940px' }); if(fun != null) { fun(); --- ! ERROR !! } }.bind(this) }); }, maximize: function() { $(this.elContainer).show(); new Effect.Morph(this.elContainer, { style: 'left: 0px;', duration: 0.5 }); }, doit: function () { } }); (i pasted in:http://pastie.org/391563 to view better) By the way - this works fine: function foo(){ alert('foo');} function bar(fn){ if(fn != null) { fn(); } else { alert('bar'); }} bar(foo); // alerts 'foo' bar(); // alerts 'bar' --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Calling function passed as parameter
Hi marioosh, I did a test, and there is no error !! just at : --- ! ERROR !! -- david On 17 fév, 12:14, T.J. Crowder t...@crowdersoftware.com wrote: Hi, What's the error? -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Feb 17, 10:53 am, marioosh marioosh@gmail.com wrote: I have a problem with calling function passed as parameter in class definition. I get error when i call fun(). How can i correct this ? var Slider = Class.create({ initialize: function (elContainer) { this.elContainer = elContainer; if($(this.elContainer).getStyle('left') == '0px') { this.minimize(this.doit); } else { this.doit(); } }, minimize: function(fun) { new Effect.Morph(this.elContainer, { style: 'left: 940px;', duration: 0.3, afterFinish: function() { $(this.elContainer).setStyle({ left: '-940px' }); if(fun != null) { fun(); --- ! ERROR !! } }.bind(this) }); }, maximize: function() { $(this.elContainer).show(); new Effect.Morph(this.elContainer, { style: 'left: 0px;', duration: 0.5 }); }, doit: function () { } }); (i pasted in:http://pastie.org/391563 to view better) By the way - this works fine: function foo(){ alert('foo');} function bar(fn){ if(fn != null) { fn(); } else { alert('bar'); }} bar(foo); // alerts 'foo' bar(); // alerts 'bar' --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Calling function passed as parameter
On 17 Lut, 12:14, T.J. Crowder t...@crowdersoftware.com wrote: Hi, What's the error? -- Error: Cannot convert undefined or null to Object Slider class is a base class and doit() method is redefined in subclasses. I see that calling fun() works, but i get error in redefined method doit(). Redefinition here: var SliderCategory = Class.create(Slider, { initialize: function($super, elContainer) { $super(elContainer); }, doit: function() { new Ajax.Updater(this.elContainer,'category_add.php',{ onCreate: function () { }.bind(this), onComplete: function(request,result) { }.bind(this) }); }, ... } Error is in prototype.js: (cut)... Ajax.Updater = Class.create(Ajax.Request, { initialize: function($super, container, url, options) { this.container = {// - ERROR Cannot convert undefined or null to Object success: (container.success || container), failure: (container.failure || (container.success ? null : container)) }; options = Object.clone(options); var onComplete = options.onComplete; options.onComplete = (function(response, json) { this.updateContent(response.responseText); if (Object.isFunction(onComplete)) onComplete(response, json); }).bind(this); $super(url, options); }, (cut)... I've pasted in pastie.org too for better view --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Calling function passed as parameter
Hi craig, I've tested the following code, and it works in FF3 !! !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http:// www.w3.org/TR/xhtml1/DTD/... html xmlns=http://www.w3.org/1999/xhtml; head script src=prototype.js type=text/javascript/script script src=scriptaculous.js type=text/javascript /script /head body div id=myId input id=bt1 type=submit/input input id=bt2 type=submit/input /div script type=text/javascript var Slider = Class.create({ initialize: function (elContainer) { this.elContainer = elContainer; if($(this.elContainer).getStyle('left') == '0px') { this.minimize(this.doit); } else { this.doit(); } }, minimize: function(fun) { new Effect.Morph(this.elContainer, { style: 'left: 940px;', duration: 0.3, afterFinish: function() { $(this.elContainer).setStyle({ left: '-940px' }); if(fun != null) { fun(); //--- ! yes, this works!, NO errors !! } }.bind(this) }); }, maximize: function() { $(this.elContainer).show(); new Effect.Morph(this.elContainer, { style: 'left: 0px;', duration: 0.5 }); }, doit: function () { } }); // BUT IN SUBCLASSES errors: var SliderCategory = Class.create(Slider, { initialize: function($super, elContainer) { $super(elContainer); }, doit: function() { new Ajax.Updater(this.elContainer,'category_add.php',{ onCreate: function () { }.bind(this), onComplete: function(request,result) { }.bind(this) }); } }); Ajax.Updater = Class.create(Ajax.Request, { initialize: function($super, container, url, options) { this.container = {// - ERROR Cannot convert undefined or null to Object success: (container.success || container), failure: (container.failure || (container.success ? null : container)) }; alert($H(this.container).inspect()); options = Object.clone(options); var onComplete = options.onComplete; options.onComplete = (function(response, json) { this.updateContent(response.responseText); if (Object.isFunction(onComplete)) onComplete(response, json); }).bind(this); $super(url, options); } }); new SliderCategory('myId'); /script /body /html I did not modify anything, and the alert($H(this.container).inspect ()); return: #Hash:{'success': 'myId', 'failure': 'myId'} without no error -- david On 17 fév, 13:12, marioosh marioosh@gmail.com wrote: On 17 Lut, 12:14, T.J. Crowder t...@crowdersoftware.com wrote: Hi, What's the error? -- Error: Cannot convert undefined or null to Object Slider class is a base class and doit() method is redefined in subclasses. I see that calling fun() works, but i get error in redefined method doit(). Redefinition here: var SliderCategory = Class.create(Slider, { initialize: function($super, elContainer) { $super(elContainer); }, doit: function() { new Ajax.Updater(this.elContainer,'category_add.php',{ onCreate: function () { }.bind(this), onComplete: function(request,result) { }.bind(this) }); }, ... } Error is in prototype.js: (cut)... Ajax.Updater = Class.create(Ajax.Request, { initialize: function($super, container, url, options) { this.container = { // - ERROR Cannot convert undefined or null to Object success: (container.success || container), failure: (container.failure || (container.success ? null : container)) }; options = Object.clone(options); var onComplete = options.onComplete; options.onComplete = (function(response, json) { this.updateContent(response.responseText); if (Object.isFunction(onComplete)) onComplete(response, json); }).bind(this); $super(url, options); }, (cut)... I've pasted in pastie.org too for better view --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Calling function passed as parameter
Hi, I'll bet this is the problem line: this.minimize(this.doit); You're assuming that 'this' is somehow bound to 'doit' when you pass the reference. It isn't. When you later call fun(), 'this' is not set to what you think it is within the call. JavaScript functions are functions, not methods. More here: http://blog.niftysnippets.org/2008/03/mythical-methods.html http://blog.niftysnippets.org/2008/04/you-must-remember-this.html http://prototypejs.org/api/function/bind http://www.alistapart.com/articles/getoutbindingsituations HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Feb 17, 1:16 pm, marioosh marioosh@gmail.com wrote: On 17 Lut, 13:30, david david.brill...@gmail.com wrote: Hi craig, I've tested the following code, and it works in FF3 !! Test this (http://pastie.org/391632) - it does not work in FF: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/...; html xmlns=http://www.w3.org/1999/xhtml; head script type=text/javascript src=resources/scriptaculous- js-1.8.2/lib/prototype.js /script script type=text/javascript src=resources/scriptaculous- js-1.8.2/src/scriptaculous.js /script /script /head body div id=myId input id=bt1 type=submit/input input id=bt2 type=submit/input /div script type=text/javascript var Slider = Class.create({ initialize: function (elContainer) { this.elContainer = elContainer; this.minimize(this.doit); }, minimize: function(fun) { new Effect.Morph(this.elContainer, { style: 'left: 940px;', duration: 0.3, afterFinish: function() { $(this.elContainer).setStyle({ left: '-940px' }); if(fun != null) { fun(); } }.bind(this) }); }, doit: function () { } }); var SliderCategory = Class.create(Slider, { initialize: function($super, elContainer) { $super(elContainer); }, doit: function() { new Ajax.Updater(this.elContainer,'category_add.php',{ onCreate: function () { }.bind(this), onComplete: function(request,result) { }.bind(this) }); } }); new SliderCategory('myId'); /script /body /html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Calling function passed as parameter
Hi marioosh, TJ is right, when you call the doit function via fun, the 'this' value is equal to Object window. So you need a little more binding to resolve your problem: replace the call to this.minimize(this.doit); by this.minimize (this.doit.bind(this)); -- david On 17 fév, 15:33, T.J. Crowder t...@crowdersoftware.com wrote: Hi, I'll bet this is the problem line: this.minimize(this.doit); You're assuming that 'this' is somehow bound to 'doit' when you pass the reference. It isn't. When you later call fun(), 'this' is not set to what you think it is within the call. JavaScript functions are functions, not methods. More here: http://blog.niftysnippets.org/2008/03/mythical-methods.htmlhttp://blog.niftysnippets.org/2008/04/you-must-remember-this.htmlhttp://prototypejs.org/api/function/bindhttp://www.alistapart.com/articles/getoutbindingsituations HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Feb 17, 1:16 pm, marioosh marioosh@gmail.com wrote: On 17 Lut, 13:30, david david.brill...@gmail.com wrote: Hi craig, I've tested the following code, and it works in FF3 !! Test this (http://pastie.org/391632) - it does not work in FF: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/...; html xmlns=http://www.w3.org/1999/xhtml; head script type=text/javascript src=resources/scriptaculous- js-1.8.2/lib/prototype.js /script script type=text/javascript src=resources/scriptaculous- js-1.8.2/src/scriptaculous.js /script /script /head body div id=myId input id=bt1 type=submit/input input id=bt2 type=submit/input /div script type=text/javascript var Slider = Class.create({ initialize: function (elContainer) { this.elContainer = elContainer; this.minimize(this.doit); }, minimize: function(fun) { new Effect.Morph(this.elContainer, { style: 'left: 940px;', duration: 0.3, afterFinish: function() { $(this.elContainer).setStyle({ left: '-940px' }); if(fun != null) { fun(); } }.bind(this) }); }, doit: function () { } }); var SliderCategory = Class.create(Slider, { initialize: function($super, elContainer) { $super(elContainer); }, doit: function() { new Ajax.Updater(this.elContainer,'category_add.php',{ onCreate: function () { }.bind(this), onComplete: function(request,result) { }.bind(this) }); } }); new SliderCategory('myId'); /script /body /html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Calling function passed as parameter
I see...yes, that works! thanks very much!! :D ps: sorry for my english ;) On 17 Lut, 18:41, david david.brill...@gmail.com wrote: Hi marioosh, TJ is right, when you call the doit function via fun, the 'this' value is equal to Object window. So you need a little more binding to resolve your problem: replace the call to this.minimize(this.doit); by this.minimize (this.doit.bind(this)); -- david --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---