-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
69 lines (52 loc) · 1.23 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
62
63
64
65
66
67
68
/**
* Module dependencies.
*/
try {
var Emitter = require('emitter');
} catch (e) {
var Emitter = require('component-emitter');
}
var proto = require('./proto');
var statics = require('./static');
/**
* 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;
this.model.emit('construct', this, attrs);
}
// mixin emitter
Emitter(model);
// statics
model.modelName = name;
model._base = '/' + name.toLowerCase() + 's';
model.attrs = {};
model.validators = [];
model._headers = {};
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;
}