/
define-helpers.js
108 lines (100 loc) · 2.77 KB
/
define-helpers.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"use strict";
var define = require("can-define");
var canReflect = require("can-reflect");
var queues = require("can-queues");
var dev = require("can-log/dev/dev");
var ensureMeta = require("../ensure-meta");
var defineHelpers = {
// returns `true` if the value was defined and set
defineExpando: define.expando,
reflectSerialize: function(unwrapped){
var constructorDefinitions = this._define.definitions;
var defaultDefinition = this._define.defaultDefinition;
this.forEach(function(val, name){
var propDef = constructorDefinitions[name];
if(propDef && typeof propDef.serialize === "function") {
val = propDef.serialize.call(this, val, name);
}
else if(defaultDefinition && typeof defaultDefinition.serialize === "function") {
val = defaultDefinition.serialize.call(this, val, name);
} else {
val = canReflect.serialize(val);
}
if(val !== undefined) {
unwrapped[name] = val;
}
}, this);
return unwrapped;
},
reflectUnwrap: function(unwrapped){
this.forEach(function(value, key){
if(value !== undefined) {
unwrapped[key] = canReflect.unwrap(value);
}
});
return unwrapped;
},
log: function(key) {
var instance = this;
var quoteString = function quoteString(x) {
return typeof x === "string" ? JSON.stringify(x) : x;
};
var meta = ensureMeta(instance);
var allowed = meta.allowedLogKeysSet || new Set();
meta.allowedLogKeysSet = allowed;
if (key) {
allowed.add(key);
}
meta._log = function(event, data) {
var type = event.type;
if (
type === "can.onPatches" || (key && !allowed.has(type)) ||
type === "can.keys" || (key && !allowed.has(type))
) {
return;
}
if (type === "add" || type === "remove") {
dev.log(
canReflect.getName(instance),
"\n how ", quoteString(type),
"\n what ", quoteString(data[0]),
"\n index ", quoteString(data[1])
);
} else {
// log `length` and `propertyName` events
dev.log(
canReflect.getName(instance),
"\n key ", quoteString(type),
"\n is ", quoteString(data[0]),
"\n was ", quoteString(data[1])
);
}
};
},
deleteKey: function(prop){
var instanceDefines = this._instanceDefinitions;
if(instanceDefines && Object.prototype.hasOwnProperty.call(instanceDefines, prop) && !Object.isSealed(this)) {
delete instanceDefines[prop];
queues.batch.start();
this.dispatch({
type: "can.keys",
target: this
});
var oldValue = this._data[prop];
if(oldValue !== undefined) {
delete this._data[prop];
delete this[prop];
this.dispatch({
type: prop,
target: this,
patches: [{type: "delete", key: prop}],
},[undefined,oldValue]);
}
queues.batch.stop();
} else {
this.set(prop, undefined);
}
return this;
}
};
module.exports = defineHelpers;