-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
62 lines (46 loc) · 1.08 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Module dependencies.
*/
var proto = require('./proto')
, statics = require('./static')
, Emitter = require('emitter');
/**
* Expose `createModel`.
*/
module.exports = createModel;
/**
* Create a new model constructor with the given `name`.
*
* @param {String} name
* @return {Function}
* @api public
*/
function createModel(name) {
if ('string' != typeof name) throw new TypeError('model name required');
/**
* Initialize a new model with the given `attrs`.
*
* @param {Object} attrs
* @api public
*/
function model(attrs) {
if (!(this instanceof model)) return new model(attrs);
attrs = attrs || {};
this._callbacks = {};
this.attrs = attrs;
this.dirty = attrs;
}
// mixin emitte
Emitter(model);
// statics
model.modelName = name;
model.base = '/' + name.toLowerCase();
model.attrs = {};
model.validators = [];
for (var key in statics) model[key] = statics[key];
// prototype
model.prototype = {};
model.prototype.model = model;
for (var key in proto) model.prototype[key] = proto[key];
return model;
}