Created
May 8, 2010 18:28
-
-
Save jeena/394693 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<script type="text/javascript"> | |
var http = (function () { | |
var PublicInterface = {} | |
function request (verb, uri, payload) { | |
var uri = uri || "/", | |
payload = payload || "" | |
if (payload) | |
payload = " \n\n" + payload | |
console.log(verb + " " + uri + " HTTP/1.1" + payload) | |
} | |
PublicInterface.get = function (uri, payload) { | |
request("GET", uri, payload) | |
} | |
PublicInterface.post = function (uri, payload) { | |
request("POST", uri, payload) | |
} | |
PublicInterface.put = function (uri, payload) { | |
request("PUT", uri, payload) | |
} | |
PublicInterface.del = function (uri, payload) { | |
request("DELETE", uri, payload) | |
} | |
return PublicInterface | |
})() | |
var ActiveRecord = (function() { | |
function ActiveRecordPrototype () {} | |
ActiveRecordPrototype.prototype = { | |
"save" : function () { | |
http.post( this.getURI(), JSON.stringify(this) ) | |
}, | |
"remove" : function () { | |
http.del( this.getURI() ) | |
} | |
} | |
return (function () { | |
var args = {}; | |
var uritemplate = args.uritemplate || "", | |
constructor = args.constructor || function() {}, | |
prototype = args.prototype || {}, | |
identifyingKey = function() { return this.uritemplate.match( /{(.+)}/ )[1] } | |
function getURI (id) { | |
var id = id || this[identifyingKey] | |
id = id.toString().toLowerCase() | |
return uritemplate.replace(/{.+}/, id) | |
} | |
constructor.prototype = new ActiveRecordPrototype() | |
constructor.prototype.constructor = constructor | |
constructor.prototype.getURI = getURI | |
for (var property in prototype) { | |
if (prototype.hasOwnProperty(property)) | |
constructor.prototype[property] = prototype[property] | |
} | |
constructor.find = function (id) { | |
http.get( getURI(id) ) | |
return new constructor(id) // das muss natürlich ausgebaut werden | |
} | |
return constructor | |
}) | |
})() | |
var Person = ActiveRecord(); | |
Person.prototype.uritemplate = "/persons/{name}.json"; | |
Person.prototype.toString = function () { | |
return "<Person '" + this.name + "'>" | |
} | |
var myNumber = ActiveRecord({ | |
uritemplate : "/numbers/{value}.json", | |
constructor : function (n) { | |
this.value = n || 0 | |
}, | |
prototype : { | |
toString : function () { | |
return "<myNumber " + this.value + ">" | |
} | |
} | |
}) | |
var jeena = new Person("Jeena"), | |
clynx = new Person("clynx"), | |
steffen = Person.find("Steffen"), | |
unknown = new Person(), | |
zero = new myNumber(), | |
one = new myNumber(1), | |
two = new myNumber(2), | |
thirteen = myNumber.find(13) | |
jeena.save() | |
jeena.remove() | |
clynx.save() | |
clynx.remove() | |
steffen.save() | |
steffen.remove() | |
unknown.save() | |
unknown.remove() | |
zero.save() | |
zero.remove() | |
one.save() | |
one.remove() | |
two.save() | |
two.remove() | |
thirteen.save() | |
thirteen.remove() | |
console.log("\nUnd nun die Objekte zum inspizieren:\n\n") | |
console.log("Jeena: ", jeena) | |
console.log("Clynx: ", clynx) | |
console.log("Steffen: ", steffen) | |
console.log("unknown: ", unknown) | |
console.log("0: ", zero) | |
console.log("1: ", one) | |
console.log("2: ", two) | |
console.log("13: ", thirteen) | |
Person.prototype.burp = function () { | |
console.log(this.name + " says: Burrrp") | |
} | |
jeena.burp() | |
clynx.burp() | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment