forked from canjs/can-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributes.js
41 lines (35 loc) · 1.09 KB
/
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
37
38
39
40
41
var assign = require("can-util/js/assign/assign");
var define = require("can-define");
var each = require("can-util/js/each/each");
function addToObservedAttrs(Element, prop) {
if(!Element._observedAttributes) {
Element._observedAttributes = [];
}
Element._observedAttributes.push(prop);
}
var decorator = function(Type){
var definitions = Type.prototype._define.definitions,
dataInitializers = Type.prototype._define.dataInitializers,
computedInitializers = Type.prototype._define.computedInitializers;
each(definitions, function(definition, property){
var attrDefinition = definition.attribute;
if(attrDefinition) {
addToObservedAttrs(Type, property);
var newDefinition = assign(definition, {
set: function(val){
var hasChanged = this._data[property] !== val;
if(hasChanged) {
this._data[property] = val;
this.setAttribute(property, val);
}
},
get: function(){
return this._data[property];
}
});
define.property(Type.prototype, property, newDefinition,
dataInitializers, computedInitializers);
}
});
};
module.exports = decorator;