I just thought of a simple solution that would not require messing with the transport object. I posted a comment on Tobie's ticket (http://dev.rubyonrails.org/ticket/7295#comment:15) to the same effect, but I'll post it here for others to see as well.

Why not eval the X-JSON header, then, if Content-type is 'application/json', eval it as well and Object.extend the X-JSON eval'ed response! This is more similar to Tobie's original suggestion, only rather than replacing it you would extend it.

This solution has the following advantages:

  • Backwards compatibility with X-JSON response being the second argument to onXXX
  • Won't break cases where bind was used to add additional arguments to onXXX
  • Doesn't require writing to the transport object (or cloning)
  • Keeps the API simple by using one object argument to handle both cases
  • Users can migrate their code to Content-type without changing the JS code
  • No data is lost unless properties in the Content-type object override properties in the X-JSON object. This is the only danger, but I think it is acceptable.

So a response like:
---------------
X-JSON: ({status: "go"})
Content-type: application/json

({data: ['one','two','ten']})

----------------

Could be used like so:
----------------

Ajax.Responders.register({
    onSuccess: function(xhr,json){
        if(json.status && json.status != 'go'){ alert('Error!'); }
    }
});
new Ajax.Request(page,{
    onSuccess: function(xhr,json){
        $('count').update($A(json.data).map(function(item){ return '<li>'+item.camelize()+'</li>'; }).join(''));
    }
});

Thoughts? I personally prefer this over my original suggestion which was to add a new property to the transport, mainly for simplicity.

Colin


--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-spinoffs@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to