[mochikit] Re: Django doXHR Headers and Guidance

2008-05-13 Thread Szaijan

Thanks Bob, the combination of using MochiKit's eval and the correct
name for responseText (as opposed to response_text) fixed the issue.
Here's the final code, as a working example of a MochiKit AJAX fn
calling a Django view function:

selectEntity : function (entity)
{
try
{
var url = '/' + ['visionary', this.model.url ?
this.model.url : this.cn, 'select', '?xhr'].join('/');
var q = [this.app, this.cn, 'id'].join('_') + '=' +
entity.pk;
var r = {method: 'POST',
mimeType: 'application/javascript',
headers: [['Content-Type', 'application/x-www-
form-urlencoded']],
sendContent: q};
var d = MochiKit.Async.doXHR(url, r);
d.addCallback(MochiKit.Base.bind(function (result) {
try
{
var r =
MochiKit.Async.evalJSONRequest(result);
var response = r;
if(typeof(r) != 'undefined')
{
var consoleMessage =
r.initial_console_message;
 
MochiKit.Base.map(MochiKit.Base.bind(function (index) {
consoleMessage += br+ index +
:  + r[index];
var rowId = [this.app, this.cn,
r[index], 'row'].join('_');
 
this.setRowHighlight(MochiKit.DOM.getElement(rowId),
 index ==
'newId' ? true : false);
}, this), ['newId', 'oldId']);
this.updateConsole(consoleMessage);
}
else
{
throw new Error('Failed to resolve XHR
Response. Result: ' + result);
}
}
catch(e)
{
MochiKit.Logging.logError('selectEntity
Callback fn ' + e);
}
}, this));
d.addErrback(MochiKit.Base.bind(function (result) {
MochiKit.Logging.log('Callback Request: ' +
result);
}, this));
}
catch(e)
{
MochiKit.Logging.logError('selectEntity(' + entity + ') '
+ e);
}
return false;
}


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: Django doXHR Headers and Guidance

2008-05-12 Thread Bob Ippolito

mimeType: 'application/javascript' doesn't do what you think it does,
it's the overrideMimeType on the XHR. For JSON it doesn't matter since
it only uses responseText, I think it's only useful for setting it to
XML.

I think your problem is that XMLHttpRequest has a responseText
attribute, not response_text.

However you should probably just use MochiKit's evalJSONRequest
instead of doing the eval yourself.

-bob

On Mon, May 12, 2008 at 9:07 AM, Szaijan [EMAIL PROTECTED] wrote:

  Hi Folks,

  I have a Django application with a scratch-built AJAX Javascript layer
  that works well on Safari and Firefox, but not on IE.  I am attempting
  to rewrite it using MochiKit, but am having some issues with the doXHR
  function, in that I seem to be getting back an empty XHR from my
  Django Python script.  I have a few questions:

  1) Can anyone see what I'm doing wrong in the MochKit code?  As I
  said, I get back an empty XHR.  Am I accessing the return XHR
  incorrectly (should be the result argument in my callback, right?)
  When I use typeof(result) I get the expected XHR type.
  result.toJSONString() yields {}.

  2) In the first code sample, I am able to access
  ajaxRequest.response_text, which should be the same as
  resulot.response_text in the MochiKit example, but the result appears
  to be empty.  Do I need to do something different to access the fields
  of the doXHR result argument?

  3) Am I getting back an empty result because doXHR sets some default
  headers for the request that are incompatible with the Django return
  type?  What would be a good set of headers to use?

  The generic code that works looks like:

 selectEntity : function (entity)
 {
 try
 {
 var url = '/visionary/game/select/?xhr';
 var eId = 'harp_game_id';
 var getData = eId + '=' + escape(entity.pk);
 var ajaxRequest = this.createRequest(); // Creates a
  generic, platform appropriate XHR
 ajaxRequest.open(POST, url, true);
 ajaxRequest.onreadystatechange =
  MochiKit.Base.bind(function ()
 {
 try
 {
 if(ajaxRequest.readyState == 4)
 {
 if(ajaxRequest.status == 200)
 {
 var response = eval( ( +
  ajaxRequest.responseText + ));
 if (response.success)
 {
 var consoleMessage =
  response.initial_console_message;

  MochiKit.Base.map(MochiKit.Base.bind(function (index) {
 consoleMessage += br+
  index + :  + response[index];
 var rowId = [this.app,
  this.cn, response[index], 'row'].join('_');

  this.setRowHighlight(MochiKit.DOM.getElement(rowId),
  index ==
  'newId' ? true : false);
 }, this), ['newId', 'oldId']);

  this.updateConsole(consoleMessage);
 }
 else
 {
 this.errorsToConsole(response);
 }
 return true;
 }
 }
 }
 catch(e)
 {
 MochiKit.Logging.logError('selectEntity
  Callback fn ' + e);
 }
 return false;
 }, this);
 ajaxRequest.setRequestHeader(Content-Type, application/
  x-www-form-urlencoded);
 ajaxRequest.send(getData);
 return true;
 }
 catch(e)
 {
 MochiKit.Logging.logError('selectEntity(' + entity + ') '
  + e);
 }
 return false;
 }

  The MochiKit heavy code I can't get a response from looks like:

 selectEntity : function (entity)
 {
 try
 {
 var url = '/visionary/game/select/?xhr';
 var q = 'harp_game_id=' + entity.pk;
 var r = {method: 'POST',
 mimeType: 'application/javascript',
 headers: [['Content-Type', 'application/x-www-
  form-urlencoded'],
 ['Accept', 'application/
  json']],
  sendContent: q};
 var d = MochiKit.Async.doXHR(url, r);
 d.addCallback(MochiKit.Base.bind(function (result) {
 try
 {
 MochiKit.Logging.log('Callback Request: ' +
  result);
 var response = eval( ( +
  result.response_text + ));
 if(typeof(response) != 'undefined')
 {