fyi - this works great! In my case, I am simply looking for a reference to the attributes of the json object that was returned - but thanks for the comments. here's how I used this technique (a simplified version where we populate an img tag with a thumbnail - which can populate another block with the same thumb when clicked)
function clickHandler(evt){ $('imgtag_we_want_to_populate').src = this.thumb; } function stateChanged(response){ var json = response.responseText.evalJSON(); for (i=0; i< {json_length_variable} ;i++){ var clickable_thumbnail = 'id_of_clickable_element'+i; var response_user_thumb = json['message'][i].thumb; $(clickable_thumbnail).innerHTML = '<img src="'+ response_user_thumb +'">'; // in the callback, I'm passing a reference to the specific row being dealt with $(clickable_thumbnail).observe('click', clickHandler.bind(json['message'][i])); } } On Oct 17, 4:44 pm, "T.J. Crowder" <[EMAIL PROTECTED]> wrote: > Hi again, > > I will just mention that if you start using logic objects and such on > the client side where the handler is a "method" of the logic object, > then this becomes more problematic, since you have two different > things you want to do with "this": Refer to the logic instance, and > refer to the 'json' object. Not a problem if you don't intend to do > that, but if you do, Prototype makes it easy to both preserve "this" > AND pass an extra parameter to your event handler after the event > object: Function.bindAsEventListener[1]. It's pretty specialized, > you don't find you need it much, but if you want to set up an event > handler and pass it extra parameters, this is what you need. > [1]http://www.prototypejs.org/api/function/bindaseventlistener > > Assuming we're operating in the context where you have 'json' and > where "this" currently refers to your logic instance, here's how you > hook it up: > > $('myelement').observe('click', > this.clickHandler.bindAsEventListener(this, json)); > > When clickHandler() gets triggered, "this" will be the logic instance, > the first parameter will be the event object, and the second parameter > will be 'json': > > function clickHandler(evt, json) > { > // Get the glarb from json > var glarb = json.glarb; > > // Do something with it using another "method" of this instance > this.doSomething(glarb); > > } > > FWIW, > -- > T.J. Crowder > tj / crowder software / com > > On Oct 17, 8:51 pm, "suki rosen" <[EMAIL PROTECTED]> wrote: > > > that's great. thanks - I'm glad I was on the right track there. This seems > > very useful for dealing with ajax data. > > > On Fri, Oct 17, 2008 at 3:33 PM, T.J. Crowder <[EMAIL PROTECTED]>wrote: > > > > Hi, > > > > > Is this what bind() is for? Can I *bind* my json object to a function > > > > that refers back to the original object? > > > > FWIW, I'd say it's a clever use of bind(). The question of whether > > > it's what bind() is *for* is kind of moot. (I'd say, on balance, yes > > > it is -- bind() is for binding functions to objects [or maybe vice- > > > versa]. This isn't the way you normally see it used, but that's a > > > different matter...) So yeah, if you don't need the context ("this") > > > for some other purpose, no reason you can't do it like that. > > > > Once you're at the point where you have your JSON data turned back > > > into an object (I'll call it 'json'), hooking up an event handler > > > would look like this: > > > > $('someElement').observe('click', clickHandler.bind(json)); > > > > Then when someone clicks that element and your clickHandler() function > > > is called, you have access to 'json' within your function as "this": > > > > function clickHandler(evt) > > > { > > > // Get the blarg from json: > > > var blarg = this.blarg; > > > } > > > > I don't think I've seen anyone do quite that thing before, but it > > > seems like a perfectly valid use of bind(). > > > > HTH, > > > -- > > > T.J. Crowder > > > tj / crowder software / com > > > > On Oct 17, 7:49 pm, pancakes <[EMAIL PROTECTED]> wrote: > > > > I have searched around for some answer to this and I have a feeling I > > > > could make it work using bind(), but I'm not sure - I'm not exactly > > > > understanding the examples and explanations in regard to my example. > > > > > I am returning a json object from the server/ajax. I am building some > > > > page elements using the json object. I'd like to allow the user to > > > > click on the newly updated content and have that click update a > > > > different part of the page. But I'd like to reference the original > > > > json object, rather than pass all of the relevant information in the > > > > onclick. > > > > > Is this what bind() is for? Can I *bind* my json object to a function > > > > that refers back to the original object? --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---