/
model.js
159 lines (143 loc) · 4.22 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var Backbone = require('./backbone')
var _ = require('./underscore')
var map = _.map
var any = _.any
var DEFAULT_CASTS = {
string: function (v) {
return v + ''
}
, int: function (v) {
return Math.floor(+v)
}
, number: function (v) {
return +v
}
, date: function (v) {
return new Date(v)
}
, boolean: function (v) {
return !!v
}
}
module.exports = Backbone.Model.extend({
/**
* Allows derived get values. The format is:
*
* derived:
* foo:
* deps: ['bar', 'baz']
* value: (bar, baz) -> bar + ' ' + baz
*
* Your deps define which properties will be passed to the value function
* and in what order. They're also used to trigger change events for
* derived values i.e., if a dep changes the derived value will
* trigger a change too.
*/
derived: {}
/**
* Allows casting specific keys. The format is:
*
* cast:
* timestamp: (v) -> moment(v)
* bar: 'string'
* baz: 'int'
*
* You can either provide your own function or use a provided basic cast.
* The basic casts are provided by DEFAULT_CASTS and include `'string'`,
* `'int'`, `'number'`, `'date'` and `'boolean'`.
*
* Doesn't cast derived or null values.
*/
, cast: {}
/**
* Returns the model after this model in its collection. If it's not in a
* collection this will return null.
*/
, next: function () {
var model = this.collection && this.collection.after(this)
return model ? model : null
}
/**
* Returns the model before this model in its collection. If it's not in a
* collection this will return null.
*/
, prev: function () {
var model = this.collection && this.collection.before(this)
return model ? model : null
}
/**
* Returns a deep clone of the attributes object.
*/
, getAttributes: function () {
return Backbone.$.extend(true, {}, this.attributes)
}
/**
* Override get to allow default value and derived values.
*/
, get: function (key, defaultValue) {
if (this.derived[key]) {
var ret = this._derive(this.derived[key])
}
else {
var ret = Backbone.Model.prototype.get.call(this, key)
}
return ret === void 0 ? defaultValue : ret
}
/**
* Derive a value from a definition.
*/
, _derive: function (definition) {
var _this = this
var args = map(definition.deps, function (key) {
return _this.get(key)
})
return definition.value.apply(null, args)
}
/**
* Override the set method to allow for casting as data comes in.
*/
, set: function (key, val, options) {
if (typeof key === 'object') {
var attrs = key
var options = val
}
else {
var attrs = {}
attrs[key] = val
}
for (k in attrs) {
var val = attrs[k]
if (!attrs.hasOwnProperty(k) || val === null) continue
if (this.cast[k]) {
attrs[k] = this._cast(val, this.cast[k])
}
}
// Do this before derived values so any events for what was actually set
// are triggered before derived change events are fired.
var ret = Backbone.Model.prototype.set.call(this, attrs, options)
for (derived in this.derived) {
var definition = this.derived[derived]
if (!this.derived.hasOwnProperty(derived)) continue
var changed = map(definition.deps, function (key) {
return attrs.hasOwnProperty(key)
})
if (any(changed)) {
this.trigger('change:' + derived, this._derive(definition))
}
}
}
/**
* Take a value, and a casting definition and perform the cast
*/
, _cast: function (value, cast) {
return this._getCastFunc(cast)(value)
}
/**
* Given a casting definition, return a function
* that should perform the cast
*/
, _getCastFunc: function (cast) {
if (typeof cast === 'function') return cast
return DEFAULT_CASTS[cast]
}
})