-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDerivedModel.js
47 lines (47 loc) · 1.32 KB
/
DerivedModel.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
// DerivedModel.js
// Common Extension of backbone models providing utilities for derived
// model fields.
define([
'backbone'
],function(Backbone){
var parent = Backbone.Model;
return Backbone.Model.extend({
whitelist: null,
blacklist: null,
filter: function(attrs){
var obj = _.clone(attrs);
if(this.whitelist !== null)
obj = _.pick(obj, this.whitelist);
if(this.blacklist !== null)
obj = _.omit(obj, this.blacklist);
return obj;
},
toJSON: function(options){
return this.filter(this.attributes);
},
// Override this from the model, gets all attributes
// Need to return at least the new attributes, but can just extend.
refresh: function(updates, attrs){
return {};
},
update: function(){
this.set({});
},
derivedlist: null,
set: function(k,v,options){
var key = _.clone(k)
,val = _.clone(v);
var attrs;
if (typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
var tempAttrs = _.extend({}, this.attributes,attrs);
tempAttrs = _.defaults(tempAttrs, this.defaults);
_.extend(attrs, this.refresh(attrs, tempAttrs));
return parent.prototype.set.apply(this,[attrs, options]);
}
});
});