-
Notifications
You must be signed in to change notification settings - Fork 42
/
model.js
37 lines (30 loc) · 1.21 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var Model = function(name, class_methods, instance_methods) {
class_methods = class_methods || {};
instance_methods = instance_methods || {};
// The model constructor.
var model = function(attributes) {
this.attributes = attributes || {};
this.changes = {};
this.errors = new Model.Errors(this);
this.uid = [name, Model.UID.generate()].join("-")
};
// Persistence is special, remove it from class_methods.
var persistence = class_methods.persistence
delete class_methods.persistence
// Apply class methods and extend with any custom class methods. Make sure
// vitals are added last so they can't be overridden.
jQuery.extend(model, Model.Callbacks, Model.ClassMethods, class_methods, {
_name: name,
collection: [],
// Convenience method to allow a simple method of chaining class methods.
chain: function(collection) {
return jQuery.extend({}, this, { collection: collection });
}
});
// Initialise persistence with a reference to the class.
if (persistence) model.persistence = persistence(model)
// Add default and custom instance methods.
jQuery.extend(model.prototype, Model.Callbacks, Model.InstanceMethods,
instance_methods);
return model;
};