OK I get it now :)  Thanks very much for your indulgence.  Your post is
timely, of course - I just ran into another problem while trying to convert
my code into a plugin.  I'm trying to create a plugin that allows me to do
something like this:

$("input").autoSave(function(){ ajax code goes here},{options});

The problem at the moment is that if I use a common function (fn) as the
closure repository, and call the autosave function more than once with
different settings strange things happen.  To be expected, really...  I'm
thinking I need to store the fn function in a global array, creating a new
object for each element, or at least each call to the plugin but I'm not
certain.  Trial and error is all I know :)  My code looks like this so far
(createCSSClass is a function by Sam Collett):

jQuery.fn.autoSave = function(fcn,settings) {

        settings = jQuery.extend({
                delay: 600,
                beforeClass: "asBefore",
                afterClass: "asAfter"
        }, settings);
        
        createCSSClass(".asBefore", "background-color:#FFdddd");
        createCSSClass(".asAfter", "background-color:#ddFFdd"); 

        
        timer = null;
        fn = function(e) {
                if (e && e.type == 'blur') {
                        if (timer)
                                window.clearTimeout(timer);
                }
                
                if (this.value != this.title){
                        fcn();
                
jQuery(this).removeClass(settings.beforeClass).addClass(settings.afterClass);
                }
        }

  return this.each(function(){
        jQuery(this).attr("title",this.value).keyup(function() {
                if (this.value != this.title){
                        if (jQuery(this).is('.' + settings.afterClass))
jQuery(this).removeClass(settings.afterClass);
                        if (!jQuery(this).is('.' + settings.beforeClass))
jQuery(this).addClass(settings.beforeClass);                                    
                                         
                        var self = this;                        
                        timer = window.setTimeout(function() 
{fn.apply(self);},settings.delay);
                }
        }).keydown(function() {
                        if (timer) window.clearTimeout(timer);
        }).blur(fn);
        
  });
};

Any advice?




Blair Mitchelmore-2 wrote:
> 
> Functions in JavaScript run at a certain scope. A lot of the time, if 
> the function isn't a part of some Object that scope defaults to the 
> window object. All JavaScript functions also have two functions that 
> allow you to redefine the scope of a function as you call it: apply and 
> call. apply takes 2 arguments: the new scope to call the function under 
> and an array of arguments to call the function with. call does the same 
> thing but instead of an array of arguments, you just supply the 
> arguments as additional arguments to that function call.
> 
> Example:
> var scope = "Scope";
> var arg = "Arg";
> var fn = function(e) {
>     window.alert(this + " => " + e);
> };
> fn(arg); // alerts " => Arg"
> fn.apply(scope,[arg]); // alerts "Scope => Arg"
> fn.call(scope,arg); // alerts "Scope => Arg"
> 
> This allows you to redefine the this variable on the fly. Additionally, 
> the reason self is used rather than this in fn.apply(self) is because 
> 'this' changes scope once you enter a new function. So inside the 
> anonymous function defined in the setTimeout call, the scope variable 
> 'this' is likely to be the window object. So you have to save a 
> reference to the 'this' you want to use as a separate variable so it can 
> be referenced elsewhere: hence self = this followed by (inside the 
> anonymous function) fn.apply(self).
> 
> -blair
> 
> Daemach wrote:
>> Yeah that worked.  I'm not certain I understand why though :)
>>
>> It does make sense that the closure would actually have to be created
>> inside
>> the event handler but .apply(self) is a new one for me.  What exactly is
>> that doing?
>>
>>
>> Blair Mitchelmore-2 wrote:
>>> $(document).ready( function() {
>>>     var timer;
>>>     var fn = function(e) {
>>>             if (e && e.type == 'blur') {
>>>                     if (timer)
>>>                             window.clearTimeout(timer);
>>>             }
>>>             // Do stuff
>>>             alert(this.id);
>>>     }
>>>     $('#test').blur(fn).keyup(function() {
>>>                     var self = this;
>>>                     timer = window.setTimeout(function() {
>>>                             fn.apply(self);
>>>                     },2000);
>>>     }).keydown(function() {
>>>             if (timer) window.clearTimeout(timer);
>>>     });
>>> });
> 
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Gmail-style-updates-tf3269331.html#a9123839
Sent from the JQuery mailing list archive at Nabble.com.


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to