I never miss an opportunity to mention my uberuseful tiny [and only] jQuery plugin. "hitch"
http://higginsforpresident.net/js/jq.hitch.js http://higginsforpresident.net/js/jq.hitch.min.js Regards, Peter On Wed, Feb 25, 2009 at 4:58 PM, Nic Hubbard <nnhubb...@gmail.com> wrote: > > Thank you. I appreciate the explanation, and for taking the time to > thoroughly explain it! > > On Feb 25, 1:40 pm, Eric Garside <gars...@gmail.com> wrote: >> Sure. Basically apply allows you to declare the scope of the function >> you're calling, instead of letting that scope resolve normally. With >> any function, it will take on the scope of whatever encloses it. So if >> you declare a function without it being enclosed, "this" will resolve >> to "window" in almost all cases. >> >> function myFunc(){ alert(this); } >> myFunc(); // [object Window] >> >> If your function is enclosed in an object, say: >> >> var obj = { name: 'myObject', myFunc: function(){ alert >> (this.name); } }; >> obj.myFunc(); // myObject >> >> then "this" will take on the scope of the object which encloses it. >> >> Using apply, you can manually declare the scope the function will >> have. >> >> var obj1 = { name: 'obj1', myFunc: function(){ alert(this.name); }}; >> var obj2 = { name: 'obj2' }; >> obj1.myFunc.apply(obj2, []); // obj2 >> >> So the first argument of apply sets the scope, which is basically a >> fancy way of saying, tells it what to make "this" inside the function. >> The second argument of apply is an array, in which you can pass >> parameters. So: >> >> function myFunc(param1, param2, param3){ >> alert(this + ' is equal to ' + (param1 + param2 + param3)); >> >> } >> >> myFunc.apply(12, [2,4,6]); // alerts "12 is equal to 12" >> >> I hope I answered your question, but I fear I may have just rambled at >> you. :( >> >> On Feb 25, 4:24 pm, Nic Hubbard <nnhubb...@gmail.com> wrote: >> >> >> >> > Ha! That worked perfectly! Thanks, I really appreciate that, I was >> > lost. >> >> > So, could you explain, just so I know, what this did: >> > defaults.onComplete.apply(obj, []); ? >> >> > On Feb 25, 1:07 pm, Eric Garside <gars...@gmail.com> wrote: >> >> > > The problem is you're losing scope when calling onComplete. Try using >> > > the .apply method. Instead of: >> >> > > defaults.onComplete(); >> >> > > try: >> >> > > defaults.onComplete.apply(obj.get(0), []); >> >> > > That should get "this" back to what you're expecting it to be. You >> > > could also skip a step and call: >> >> > > defaults.onComplete.apply(obj, []); >> >> > > --- >> >> > > onComplete: function(){ alert(this.attr('class')); } >> >> > > I'm pretty sure that should work. IF not, let me know, and I'll play >> > > around with it locally and actually test it out. >> >> > > On Feb 25, 3:52 pm, Nic Hubbard <nnhubb...@gmail.com> wrote: >> >> > > > I was meaning when trying to call $(this) in the following >> > > > circumstance: >> >> > > > $('a.matrixStatus').matrixStatus({ >> > > > urlSuffix: '?action=status_posting', >> > > > onComplete: function() {alert('Callback worked'); alert($ >> > > > (this).attr('class'));} >> > > > }); >> >> > > > When I am trying to pass things to the custom function, using $(this) >> > > > does not work. >> >> > > > On Feb 25, 12:28 pm, brian <bally.z...@gmail.com> wrote: >> >> > > > > Something like this? (no pun intended) >> >> > > > > obj.click(function() { >> > > > > var self = $(this); >> >> > > > > ... >> >> > > > > defaults.onComplete(self); >> >> > > > > On Wed, Feb 25, 2009 at 3:11 PM, Nic Hubbard <nnhubb...@gmail.com> >> > > > > wrote: >> >> > > > > > I have built a custom callback into my plugin, here is an example: >> >> > > > > > $.fn.matrixStatus = function(options) { >> > > > > > var defaults = { >> > > > > > urlSuffix: '?action=live', >> > > > > > onComplete: function() {} >> > > > > > }; >> >> > > > > > var options = $.extend(defaults, options); >> >> > > > > > return this.each(function() { >> > > > > > var obj = $(this); >> > > > > > var itemDesc = obj.attr('rel'); >> > > > > > var itemId = obj.attr('id'); >> > > > > > var itemHref = obj.attr('href'); >> > > > > > obj.click(function() { >> > > > > > if (!itemDesc == '') { >> > > > > > var question = confirm('Are you sure you want to >> > > > > > change the status >> > > > > > of "'+itemDesc+'"?'); >> > > > > > } else { >> > > > > > var question = confirm('Are you sure you want to >> > > > > > change the >> > > > > > status?'); >> > > > > > } >> > > > > > if (question) { >> > > > > > $.ajax({ >> > > > > > type: 'POST', >> > > > > > url: itemHref + defaults.urlSuffix >> > > > > > }); >> >> > > > > > // Run our custom callback >> > > > > > defaults.onComplete(); >> >> > > > > > } >> > > > > > return false; >> >> > > > > > }); >> >> > > > > > }); >> >> > > > > > }; >> >> > > > > > For some reason when I try to use that function for a custom >> > > > > > callback, >> > > > > > it won't allow me to get the jQuery object that the plugin is >> > > > > > targeting, so using $(this) within the onComplete function doesn't >> > > > > > work and give me errors. Any idea why this would be?