-
Notifications
You must be signed in to change notification settings - Fork 3
/
backbone.attributes.js
36 lines (30 loc) · 1.54 KB
/
backbone.attributes.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
// Backbone.Attributes.js 0.7.1
// ---------------
// (c) 2014 Adam Krebs
// Backbone.Attributes may be freely distributed under the MIT license.
// For all details and documentation:
// https://github.com/akre54/Backbone.Attributes
(function (factory) {
if (typeof define === 'function' && define.amd) { define(['underscore', 'backbone', 'exports'], factory);
} else if (typeof exports === 'object') { factory(require('underscore'), require('backbone'), exports);
} else { factory(_, Backbone, (Backbone.Attributes = {})); }
}(function (_, Backbone, Attributes) {
_.extend(Attributes, Backbone.Events);
var modelMethods = ['get', 'set', 'unset', 'clear', '_validate', 'validate', 'isValid', 'has', 'changed', 'hasChanged', 'changedAttributes', 'previous', 'previousAttributes'];
var wrappedMethods = ['get', 'set', 'clear', 'changedAttributes'];
var Model = Backbone.Model.prototype;
_.each(modelMethods, function(method) {
var fn = Model[method];
var wrapper = function() {
_.defaults(this.attributes || (this.attributes = {}), _.result(this, 'defaults'));
_.each(wrappedMethods, function(m) { if (this[m]._wrapped) this[m] = Model[m]; }, this);
this.setAttribute = this.setAttributes = Model.set;
this.getAttribute = Model.get;
return fn.apply(this, arguments);
};
wrapper._wrapped = true;
Attributes[method] = _.contains(wrappedMethods, method) ? wrapper : fn;
});
Attributes.setAttribute = Attributes.setAttributes = Attributes.set;
Attributes.getAttribute = Attributes.get;
}));