/
backbone.computedfields.js
141 lines (107 loc) · 4.63 KB
/
backbone.computedfields.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
// Backbone.ComputedFields, v0.0.5
// Copyright (c)2013 alexander.beletsky@gmail.com
// Distributed under MIT license
// https://github.com/alexanderbeletsky/backbone.computedfields
(function (root, factory) {
if (typeof exports === 'object') {
var underscore = require('underscore');
var backbone = require('backbone');
module.exports = factory(underscore, backbone);
} else if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone'], factory);
}
}(this, function (_, Backbone) {
Backbone.ComputedFields = (function(Backbone, _){
var ComputedFields = function (model) {
this.model = model;
this._computedFields = [];
this.initialize();
};
ComputedFields.VERSION = '0.0.5';
_.extend(ComputedFields.prototype, {
initialize: function () {
_.bindAll(this);
this._lookUpComputedFields();
this._bindModelEvents();
this._wrapJSON();
},
_lookUpComputedFields: function () {
for (var obj in this.model.computed) {
var field = this.model.computed[obj];
if (field && (field.set || field.get)) {
this._computedFields.push({name: obj, field: field});
}
}
},
_bindModelEvents: function () {
_.each(this._computedFields, function (computedField) {
var fieldName = computedField.name;
var field = computedField.field;
var updateComputed = _.bind(function () {
var value = this._computeFieldValue(field);
this.model.set(fieldName, value, { skipChangeEvent: true });
}, this);
var updateDependent = _.bind(function (model, value, options) {
if (options && options.skipChangeEvent) {
return;
}
if (field.set) {
var fields = this._dependentFields(field.depends);
value = value || this.model.get(fieldName);
field.set.call(this.model, value, fields);
this.model.set(fields, options);
}
}, this);
this._thenDependentChanges(field.depends, updateComputed);
this._thenComputedChanges(fieldName, updateDependent);
if (this._isModelInitialized()) {
updateComputed();
}
}, this);
},
_isModelInitialized: function () {
return !_.isEmpty(this.model.attributes);
},
_thenDependentChanges: function (depends, callback) {
_.each(depends, function (name) {
if (typeof (name) === 'string') {
this.model.on('change:' + name, callback);
}
if (typeof (name) === 'function') {
name.call(this.model, callback);
}
}, this);
},
_thenComputedChanges: function (fieldName, callback) {
this.model.on('change:' + fieldName, callback);
},
_wrapJSON: function () {
this.model.toJSON = _.wrap(this.model.toJSON, this._toJSON);
},
_toJSON: function (toJSON) {
var attributes = toJSON.call(this.model);
var stripped = _.reduce(this._computedFields, function (memo, computed) {
if (computed.field.toJSON === false) {
memo.push(computed.name);
}
return memo;
},[]);
return _.omit(attributes, stripped);
},
_computeFieldValue: function (computedField) {
if (computedField && computedField.get) {
var fields = this._dependentFields(computedField.depends);
return computedField.get.call(this.model, fields);
}
},
_dependentFields: function (depends) {
return _.reduce(depends, function (memo, field) {
memo[field] = this.model.get(field);
return memo;
}, {}, this);
}
});
return ComputedFields;
})(Backbone, _);
return Backbone.ComputedFields;
}));