Permalink
Browse files

Added response.execute function

  • Loading branch information...
1 parent 0cea32c commit 861768dc956e2e640229ddb92d60f85d9a77f2c8 @jasonl committed Oct 31, 2010
Showing with 32 additions and 8 deletions.
  1. +10 −1 README.md
  2. +4 −4 application_handler-min.js
  3. +18 −3 application_handler.js
View
@@ -27,6 +27,7 @@ controller. **Note:** The script block must include the class `"handler"` so tha
this.create = function() {
POST( comments_url(), this.form.serialize() );
response.append_to('comments').on(200);
+ response.execute(someFunction).on(200);
response.replace('errorMessages').otherwise();
};
@@ -142,9 +143,17 @@ The following functions do something with the Ajax response received:
response.remove(domId)
</td>
<td>
- Removes the element with id domId from the DOM. *Javascript within the reponse body is not evaluated at present*
+ Removes the element with id domId from the DOM.
</td>
</tr>
+ <tr>
+ <td>
+ response.execute(fn)
+ </td>
+ <td>
+ Calls the function object passed to it.
+ </td>
+ </tr>
</table>
Why Rails needs this
@@ -1,15 +1,15 @@
if(typeof(ApplicationHandler)=='undefined'){RequestBuilder=function(){var self=this;this.requestFn=null;['get','put','post','delete'].each(function(method){self[method.toUpperCase()]=function(url,data){self.requestFn=function(callback){new Ajax.Request(url,{method:method,onComplete:callback,parameters:data});};};});};ResponseBuilder=function(){var self=this;self.funcs=[];this.callbackFn=function(){if(self.funcs.length==0){return function(){};}
-if(self.funcs.length==1){return self.funcs[0];}else{return function(resp){for(var i=0;i<clauses.length;i++){clauses[i](resp);}};}}
-this.clear=function(){self.funcs=[];};this.replace=function(id){self.funcs.push(function(resp){$(id).innerHTML=resp.responseText;});return self;};this.append_to=function(id){self.funcs.push(function(resp){$(id).insert(resp.responseText);});return self;}
-this.rjs=function(){self.funcs.push(function(resp){eval(resp.responseText);});return self;};this.remove=function(id){self.funcs.push(function(resp){$(id).remove();});return self;};this.on=function(status){var action=self.funcs.pop();self.funcs.push(function(resp){if(resp.status==status){action(resp);return true;}
+if(self.funcs.length==1){return self.funcs[0];}else{return function(resp){for(var i=0;i<self.funcs.length;i++){self.funcs[i](resp);}};}}
+this.clear=function(){self.funcs=[];};this.replace=function(id){self.funcs.push(function(resp){$(id).innerHTML=resp.responseText;});return self;};this.append_to=function(id){self.funcs.push(function(resp){$(id).insert(resp.responseText);return true;});return self;};this.execute=function(fn){if(typeof(fn)=="function"){self.funcs.push(function(resp){fn();return true;});}else{throw"Invalid callback type";}
+return self;};this.rjs=function(){self.funcs.push(function(resp){eval(resp.responseText);return true;});return self;};this.remove=function(id){self.funcs.push(function(resp){return $(id).remove();});return self;};this.on=function(status){var action=self.funcs.pop();self.funcs.push(function(resp){if(resp.status==status){action(resp);return true;}
return false;});};this.otherwise=function(){var action=self.funcs.pop();var clauses=self.funcs.clone();self.funcs=[function(resp){var executed=false;for(var i=0;i<clauses.length;i++){executed=executed||clauses[i](resp);}
if(!executed){action(resp);}}];};};ApplicationHandler=function(scriptElement){base_url='/';scriptElement=$(scriptElement);var response=new ResponseBuilder;var request=new RequestBuilder;var classNameRegex=/function (.{1,})\(/;var results=(classNameRegex).exec(scriptElement.innerHTML);if(results&&results.length>1){var pluralName=results[1].replace('Handler','');var plural_name=results[1].replace('Handler','').underscore();var singularName=pluralName.replace(/s$/,'');var singular_name=plural_name.replace(/s$/,'');}else{throw"No ClassName found";}
var routeFunctions=buildRouteSetFunctions();for(i=0;i<routeFunctions.length;i++){eval(routeFunctions[i]);}
function GET(url,data){request.GET(url,data);}
function POST(url,data){request.POST(url,data);}
function PUT(url,data){request.PUT(url,data);}
function DELETE(url,data){request.DELETE(url,data);}
-eval("var derived = new "+scriptElement.innerHTML);var bindCallback=function(fnName,domClass){if(derived[fnName]){$(document).observe('click',function(e){var el=e.findElement("."+domClass);if(el){response.clear();derived[fnName].call(el);request.requestFn(response.callbackFn());e.stop();}});}}
+eval("var derived = new "+scriptElement.innerHTML);var bindCallback=function(fnName,domClass){if(derived[fnName]){$(document).observe('click',function(e){var el=e.findElement("."+domClass);if(el){response.clear();derived[fnName].call(el);request.requestFn(response.callbackFn());Event.stop(e);return false;}});}}
generateBindingParams(bindCallback);return derived;function buildRouteSetFunctions(){var routeSet=[];routeSet[0]="function "+singular_name+"_url(id) { return base_url + plural_name + '/' + id; }";routeSet[1]="function "+plural_name+"_url() { return base_url + plural_name; }";routeSet[2]="function new_"+singular_name+"_url() { return base_url + plural_name + '/new'; }";routeSet[3]="function edit_"+singular_name+"_url() { return base_url + plural_name + '/' + id + '/edit'; }";return routeSet;}
function generateBindingParams(callback){callback('_new','new'+singularName);callback('create','create'+singularName);callback('edit','edit'+singularName);callback('update','update'+singularName);callback('index','index'+pluralName);callback('show','show'+singularName);callback('destroy','destroy'+singularName);}};}
$(document).observe("dom:loaded",function(){$$('script.handler').map(function(se){return ApplicationHandler(se);});});
@@ -65,20 +65,34 @@ if(typeof(ApplicationHandler) == 'undefined') {
this.append_to = function(id) {
self.funcs.push(function(resp) {
$(id).insert(resp.responseText);
+ return true;
});
return self;
- }
+ };
+
+ this.execute = function(fn) {
+ if (typeof(fn) == "function") {
+ self.funcs.push( function(resp) {
+ fn();
+ return true;
+ });
+ } else {
+ throw "Invalid callback type";
+ }
+ return self;
+ };
this.rjs = function() {
self.funcs.push( function(resp) {
eval(resp.responseText);
+ return true;
});
return self;
};
this.remove = function(id) {
self.funcs.push( function(resp) {
- $(id).remove();
+ return $(id).remove();
});
return self;
};
@@ -144,7 +158,8 @@ if(typeof(ApplicationHandler) == 'undefined') {
response.clear();
derived[fnName].call(el);
request.requestFn(response.callbackFn());
- e.stop();
+ Event.stop(e);
+ return false;
}
});
}

0 comments on commit 861768d

Please sign in to comment.