Tuesday, August 22nd, 2006

Prototype Extension: Dynamic Script Pattern Support

Category: Prototype

Cody Swann was working on a web application that was using the Dynamic Script Pattern, which Dojo has excellent support for, but Prototype didn’t.

Cody then extended Prototype to support ScriptSrcTransport similarly to how Dojo does it.

The code below support the Simple, Polling and JSONP and JSON Callbacks described in the Dojo book.

javascript

  1. Ajax = Object.extend(Ajax,{
  2.     getTransport: function(transport)
  3.     {
  4.         return Try.these(
  5.             function() {return new transport();},
  6.             function() {return new XMLHttpRequest();},
  7.             function() {return new ActiveXObject('Msxml2.XMLHTTP');},
  8.             function() {return new ActiveXObject('Microsoft.XMLHTTP');}
  9.         ) || false;
  10.     },
  11.     activeRequestCount: 0
  12. });
  13.  
  14. Ajax.Base.prototype = Object.extend(Ajax.Base.prototype,{
  15.   setOptions: function(options) {
  16.     this.options = {
  17.       method:       'post',
  18.       asynchronous: true,
  19.       contentType:  'application/x-www-form-urlencoded',
  20.       parameters:   '',
  21.       transport: null,
  22.       checkString: '',
  23.       timeoutSeconds: 20,
  24.       jsonParamName: null
  25.     }
  26.     Object.extend(this.options, options || {});
  27.   }
  28. });
  29.  
  30. Ajax.Request.prototype = Object.extend(Ajax.Request.prototype, {
  31.   initialize: function(url, options) {
  32.     this.setOptions(options);
  33.     this.transport = Ajax.getTransport(this.options.transport);
  34.     this.request(url);
  35.   },
  36.   request: function(url) {
  37.     var parameters = this.options.parameters || '';
  38.     if (parameters.length > 0) parameters += '&_=';
  39.  
  40.     try {
  41.       this.url = url;
  42.       if (this.options.method == 'get' && parameters.length > 0)
  43.         this.url += (this.url.match(/?/) ? '&' : '?') + parameters;
  44.  
  45.       Ajax.Responders.dispatch('onCreate', this, this.transport);
  46.  
  47.       this.transport.open(this.options.method, this.url,
  48.         this.options.asynchronous,this.options.username,this.options.password,this.options);
  49.  
  50.       if (this.options.asynchronous) {
  51.         this.transport.onreadystatechange = this.onStateChange.bind(this);
  52.         setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
  53.       }
  54.  
  55.       this.setRequestHeaders();
  56.  
  57.       var body = this.options.postBody ? this.options.postBody : parameters;
  58.       this.transport.send(this.options.method == 'post' ? body : null);
  59.  
  60.     } catch (e) {
  61.       this.dispatchException(e);
  62.     }
  63.   },
  64.   evalJSON: function() {
  65.     if(this.transport.json){ return this.transport.json; }
  66.     else
  67.     {
  68.       try {
  69.         return eval('(' + this.header('X-JSON') + ')');
  70.       } catch (e) {}
  71.     }
  72.   }
  73. });

Nice work Cody!

Posted by Dion Almaer at 11:02 am
6 Comments

++++-
4 rating from 19 votes

6 Comments »

Comments feed TrackBack URI

Cody Swann… You are a genius!!! Is there nothing you can’t do?!

Comment by Brianne — August 22, 2006

Line 48 should be
try{ this.transport.open(this.options.method, this.url, this.options.asynchronous,this.options.username,this.options.password,this.options); }
catch(err){ this.transport.open(this.options.method, this.url,this.options.asynchronous); }

IE pukes if you call new Ajax.Request more than once because the XML Req object is not native and cannot take more than the maximum number of arguments.

My fault.

Comment by Cody — August 23, 2006

Interesting Finds: August 23, 2006

Trackback by Jason Haley — August 23, 2006

Very useful! You are my idol.

Comment by Mager — August 24, 2006

[…] Ajaxian » Prototype Extension: Dynamic Script Pattern Support […]

Pingback by Interaction Design Blog » Blog Archive » On Demand JavaScript — September 6, 2006

[…] I am not saying cross domain scripting isn’t a problem. But I am saying that it’s only a problem when the other domain is being malicious. From a developers perspective you can be extremely selective as to which domains you allow to script with. Say yes to Yahoo and not to bankone.secure.sadfasdfs.com. So for one developer to say to another that dynamic script pattern (here and here) is dangerous doesn’t make sense. They are overlooking the developers ability to opt out of any potential scripts which could pose a security risk. […]

Pingback by almost there » Blog Archive » Playing around with the Photagious Media Server — October 6, 2006

Leave a comment

You must be logged in to post a comment.