Right, but the function is defined outside of the scope of the dialog
... usually in an anonymous function within a window.onload observer. I
pass an object containing those anonymous functions to the intialize()
method of the dialog class and then copy them over into the class
itself. That way, a page can have more than one dialog which respond to
their own set of event handlers.
I have found out that specifying the context of the function using the
call() method of the JavaScript Function object seems to work, but I'm
not sure this is a well known feature of Javascript actions. For
example, here's a dialog with an anonymous beforeLoad function. I can't
bind() it here because this refers to the local scope and I can't bind
it to "dialog" since it's not actually initialized until after the
Dialog.prototype.initialize() function completes...
var dialog = new Dialog("test.php", { beforeLoad: function() {
alert(this.dialog_id); return null; } });
But, if I add the following to line #46 in the pastie from my original
post, things work:
this.json = this.responders["beforeLoad"].call(this);
Everything is happy. I found out about the call method of Functions in
JavaScript only after I posted my message above. I wonder if this is
common practice? Also, I notice that it is used in the prototype.js
code, so maybe this is the best way to do it?
-- Dash --
Ryan Gahl wrote:
> You can attach .bind(this) to anonymous functions...
>
> beforeOnload: function() {....stuff....}.bind(this)
>
> On 4/9/07, David Dashifen Kees <[EMAIL PROTECTED]> wrote:
>
>> Greetings all,
>>
>> I've written an object to create a lightbox-like dialog on-screen (I
>> posted about it earlier; search for "dialogish" within the group and you'll
>> find that thread). I then ran into the Control.Modal window which did
>> much of what I wanted, but was also much more complex. I didn't understand
>> all of what the developer of Control.Modal was doing so I tried to take
>> what I did understand from his class and apply it to mine. If you're
>> unfamiliar with Control.Modal, you can find it here:
>> http://livepipe.net/projects/control_modal/.
>>
>> The problem I'm facing as that the functions that are called to respond to
>> different dialog-centric events (like beforeLoad, beforeOpen, etc.) have a
>> scope that's not that of the dialog object. Code time:
>>
>> var dialog = new Dialog("test.php", {
>> beforeLoad: function() { return { window_id: $F("window_id") } }
>> }); dialog.activate();
>>
>> The above snippet shows the basics of how the dialog object is started.
>> You pass the URL of the information to be loaded
>> via Ajax.Updater as well as any custom functions which need to respond to
>> one of four different events within the dialog code: before the information
>> is loaded, before the content is shown, before the content is hidden, and
>> before the dialog container is actually removed from the DOM. The above
>> snippet would work fine, but I can imagine a time when I might want to do
>> something like this:
>>
>> var dialog = new Dialog("test.php", {
>> beforeClose: function() {
>> $(this.dialog_id).getElementsByTagName("input").each(function(input)
>> { /* ... */ })
>> }
>> }); dialog.activate();
>>
>> The above snippet doesn't work, clearly because the this keyword refers
>> to the scope of the anonymous function, unless I misunderstand something
>> crucial. So, I tried to do something like this:
>>
>> Dialog.prototype = {
>> initialize: function(href, custom_responders) {
>> /* ... do other setup stuff ... */
>>
>> if(custom_responders)
>> for(responder in custom_responders)
>> this.responders[responder] = custom_responder[responder];
>>
>> *for(responder in this.responders) this.responders
>> [responder].bind(this)*
>>
>> I thought that the above use of the bind() function would tell the the
>> responder functions which are passed within the custom_responders object to
>> use the scope of the dialog object as their scope but that doesn't seem to
>> be the case. Simply uses of this binding like the following still fail:
>>
>> var dialog = new Dialog("test.php", { beforeLoad: function() { alert(
>> this.dialog_id) } });
>>
>> For what it's worth, it alerts undefined. Here's the code:
>> http://pastie.caboo.se/52614
>>
>> Note that the usage of bind() mentioned in this post is not in the
>> pastie. My bad.
>>
>> -- Dash --
>>
>>
>>
>>
>>
>>
>
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---