Skip to content

Instantly share code, notes, and snippets.

@jaydesignhaiti
Created March 15, 2012 22:18
Math.guid = function(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
}).toUpperCase();
};
<head>
<meta charset="utf-8">
<title>Index</title>
<meta name="description" content="">
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="scripts/jquery-1.7.1.js"></script>
<script type="text/javascript" src="scripts/helpers.js"></script>
<script type="text/javascript" src="scripts/models.js"></script>
</head>
<body>
<script>
var Event = Model.create();
Event.attributes ['name', 'picture'];
var _event = Event.init({name: "document", picture: "images.jpg"});
_event.save();
var json = JSON.stringify(Event.records);
document.write(json);
</script>
</body>
</html>
if (typeof Object.create !== "function")
Object.create = function(o) {
function F() {}
F.prototype = o;
return new F();
};
var Model = {
// We’re going to create a Model object, which will be in charge of creating new models and instances
inherited: function(){},
created: function(){},
prototype: {
init: function(){}
},
create: function(){ // The create() function returns a new object, inheriting from the Model object; we’ll use this for creating new models
var object = Object.create(this);
object.parent = this;
object.prototype = object.fn = Object.create(this.prototype);
object.created();
this.inherited(object);
return object;
},
init: function(){ // The init() function returns a new object, inheriting from Model.prototype—i.e., an instance of the Model object
var instance = Object.create(this.prototype);
instance.parent = this;
instance.init.apply(instance, arguments);
return instance;
},
extend: function(o){
var extended = o.extended;
jQuery.extend(this, o);
if (extended) extended(this);
},
include: function(o){
var included = o.included;
jQuery.extend(this.prototype, o);
if (included) included(this);
}
};
/* We need a way of persisting records—i.e., of saving a reference to created instances so
we can access them later. We’ll do that using a records object, set on the Model. When
we’re saving an instance, we’ll add it to that object; when deleting instances, we’ll
remove them from the object: */
// An object of saved assets
Model.records = {};
Model.include({
newRecord: true,
create: function(){
if ( !this.id ) this.id = Math.guid();
this.newRecord = false;
this.parent.records[this.id] = this.dup();
},
destroy: function(){
delete this.parent.records[this.id];
},
update: function(){
this.parent.records[this.id] = this.dup();
},
save: function(){
this.newRecord ? this.create() : this.update();
},
dup: function(){
return jQuery.extend(true, {}, this);
},
attributes: function(){
var result = {};
for(var i in this.parent.attributes) {
var attr = this.parent.attributes[i];
result[attr] = this[attr];
}
result.id = this.id;
return result;
},
toJSON: function(){
return(this.attributes());
}
});
Model.extend({
// Find by ID, or raise an exception
find: function(id){
var record = this.records[id];
if ( !record ) throw("Unknown record");
return record.dup();
},
created: function(){ // This might belong to Model.include
this.records = {};
this.attributes = [];
},
populate: function(values){
// Reset model & records
this.records = {};
for (var i=0, il = values.length; i < il; i++) {
var record = this.init(values[i]);
record.newRecord = false;
this.records[record.id] = record;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment