Created
November 11, 2011 21:12
-
-
Save chadhutchins/1359292 to your computer and use it in GitHub Desktop.
A generic javascript class that wraps the default rails scaffolding so you can use javascript/ajax to make calls to the controllers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Resource = function(controller) { | |
// controller param required | |
var controller = controller ? controller : false; | |
if (!controller) { console.log("controller parameter required for resource"); return false; } | |
this.controller = controller; | |
}; | |
Resource.prototype = { | |
index: function(callback) { | |
var type = "GET"; | |
var url = "/"+this.controller+".json"; | |
this.call(type,url,null,callback); | |
}, | |
show: function(id,callback) { | |
// id param required | |
var id = id ? id : false; | |
if (!id) return false; | |
var type = "GET"; | |
var url = "/"+this.controller+"/"+id+".json"; | |
this.call(type,url,null,callback); | |
}, | |
new: function(callback) { | |
var type = "GET"; | |
var url = "/"+this.controller+"/new.json"; | |
this.call(type,url,null,callback); | |
}, | |
edit: function(callback) { | |
// this one doesn't make so much since as there isn't a default resource/:id/edit.json method | |
// thoughts? | |
callback(null); | |
}, | |
create: function(data,callback) { | |
// data param required | |
var data = data ? data : false; | |
if (!data) return false; | |
var type = "POST"; | |
var url = "/"+this.controller+".json"; | |
this.call(type,url,data,callback); | |
}, | |
update: function(id,data,callback) { | |
// id and data params required | |
var id = id ? id : false; | |
var data = data ? data : false; | |
if (!id || !data) return false; | |
var type = "PUT"; | |
var url = "/"+this.controller+"/"+id+".json"; | |
this.call(type,url,data,callback); | |
}, | |
destroy: function(id,callback) { | |
// id param required | |
var id = id ? id : false; | |
if (!id) return false; | |
var type = "DELETE"; | |
var url = "/"+this.controller+"/"+id+".json"; | |
this.call(type,url,null,callback); | |
}, | |
call: function(type,url,data,callback) { | |
// type and url params required | |
var type = type ? type : false; | |
var url = url ? url : false; | |
if (!type || !url) return false; | |
// jquery currently required | |
$.ajax({ | |
type: type, | |
url: url, | |
data: data, | |
success: function(data){ | |
callback(data); | |
} | |
}); | |
} | |
}; | |
var Node = new Resource("nodes"); | |
$(document).ready(function(){ | |
$("#b1").click(function(){ | |
Node.index(function(data){ | |
for (var i=0;i<data.length;i++) { | |
console.log(data[i].id+": "+data[i].name); | |
} | |
}); | |
}); | |
$("#b2").click(function(){ | |
Node.show(5,function(data) { | |
console.log(data.name); | |
}); | |
}); | |
$("#b3").click(function(){ | |
Node.new(function(data){ | |
console.log(data); | |
}); | |
}); | |
$("#b5").click(function(){ | |
Node.create({'node[name]': "New Node Sucka"},function(data){ | |
console.log(data); | |
}); | |
}); | |
$("#b6").click(function(){ | |
Node.update(11,{'node[name]': 'Holla atcha boi'},function(data){ | |
console.log(data); | |
}); | |
}); | |
$("#b7").click(function(){ | |
Node.destroy(12); | |
Node.destroy(13); | |
Node.destroy(14); | |
console.log('done ;)'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment