backbone-model-overlay.js
3.6 KB
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
define(function(require) {
'use strict';
var module = require('module');
var Backbone = require('backbone');
var _ = require('underscore');
var BackboneModelOverlay = Backbone.Model.extend({
initialize: function(data, options) {
BackboneModelOverlay.__super__.initialize.apply(this, arguments);
var parent = this._parent = options.parent;
if (parent) {
parent.on('change', function(value, parentModel, options) {
var parentChanged = this._parent.changedAttributes();
this.changed = parentChanged;
_.each(parentChanged, function(value, key) {
this.trigger('change:' + key, value, this, options);
}, this);
this.trigger('change', this, options);
}, this);
}
var override = this._override = {};
_.each(_.keys(this.attributes), function(key) {
override[key] = true;
});
},
get: function(key) {
if (this._override[key]) {
return BackboneModelOverlay.__super__.get.apply(this, arguments);
} else {
var parent = this._parent;
return parent && parent.get.apply(parent, arguments);
}
},
previous: function(attr) {
return this.previousAttributes()[attr];
},
previousAttributes: function() {
var parent = this._parent;
var data = BackboneModelOverlay.__super__.previousAttributes.apply(this, arguments);
var parentData = parent ? parent.previousAttributes.apply(parent, arguments) : null;
return _.extend({}, parentData, data);
},
changedAttributes: function() {
var parent = this._parent;
var data = BackboneModelOverlay.__super__.changedAttributes.apply(this, arguments);
var parentData = parent ? parent.changedAttributes.apply(parent, arguments) : null;
return _.extend({}, parentData, data);
},
set: function(key, value, options) {
if (!!!key) {
return this;
}
var attrs;
if (typeof key === 'object') {
attrs = key;
options = value;
} else {
(attrs = {})[key] = value;
}
options || (options = {});
var unset = options.unset;
var parent = this._parent;
if (parent) {
if (!options.overlay) {
return parent.set.apply(this, arguments);
}
var override = this._override;
_.each(attrs, function(value, key) {
if (unset) {
delete override[key];
} else {
override[key] = true;
}
});
}
return BackboneModelOverlay.__super__.set.apply(this, arguments);
},
toJSON: function(options) {
var data = BackboneModelOverlay.__super__.toJSON.apply(this, arguments);
var parent = this._parent;
if (parent) {
data = _.extend(data, parent.toJSON.apply(parent, arguments));
}
var superGet = BackboneModelOverlay.__super__.get;
_.each(this._override, function(value, key) {
data[key] = superGet.call(this, key, options);
}, this);
return data;
},
});
return BackboneModelOverlay;
});