-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.immutable.js
44 lines (38 loc) · 1.01 KB
/
backbone.immutable.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
Backbone.Immutable = (function (Backbone, _) {
var attrs = {};
var Immutable = Backbone.Model.extend({
constructor: function (attributes, options) {
Backbone.Model.prototype.constructor.call(this, arguments);
attrs = attributes;
},
get: function(key) {
return attrs[key];
},
set: function (key, val) {
var data;
if (key == null) return this;
// Handle both `"key", value` and `{key: value}` -style arguments.
if (typeof key === 'object') {
data = key;
} else {
(data = {})[key] = val;
}
_.each(data, function(value, key) {
if (_.isUndefined(attrs[key])) {
attrs[key] = value;
} else {
var msg = "You're not supposed to change this value.";
if ('warn' in console) {
console.warn(msg);
} else {
console.log(msg);
}
}
});
},
toJSON: function() {
return _.clone(attrs);
}
});
return Immutable;
})(Backbone, _);