Skip to content

Commit 1bf57b5

Browse files
committed
Fix an issue with structure event
1 parent 2cb2657 commit 1bf57b5

File tree

1 file changed

+201
-86
lines changed

1 file changed

+201
-86
lines changed

src/component.js

Lines changed: 201 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -995,105 +995,220 @@ function addStructure(path, name, model, id) {
995995
propertyType = prop.type;
996996
propertyReadOnly = prop.readOnly;
997997

998-
body = function body(value) {
999-
var search = [],
1000-
component = null,
1001-
propertyValue = null,
1002-
parentPath = '',
1003-
fullPath = '';
1004-
1005-
if (path) {
1006-
parentPath = parentPath + '.' + name;
1007-
} else {
1008-
parentPath = name;
1009-
}
1010-
fullPath = parentPath + '.' + propertyName;
998+
if (propertyType === 'array') { // in case of array, return a sub array
999+
body = function body(position, value) {
1000+
var search = [],
1001+
component = null,
1002+
runtimeArr = null,
1003+
val = null,
1004+
realVal = null,
1005+
i = 0,
1006+
length = 0,
1007+
parentPath = '',
1008+
fullPath = '';
10111009

1012-
if (typeof value === 'undefined') {
1013-
component = $db.store[model][id];
1014-
if (component) {
1015-
switch (true) {
1016-
case propertyType.indexOf('@') !== -1:
1017-
propertyValue = get(getStructureValue(model, id, fullPath));
1018-
break;
1019-
case propertyType === 'date':
1020-
propertyValue = new Date(getStructureValue(model, id, fullPath));
1021-
break;
1022-
case propertyType === 'array':
1023-
propertyValue = new _Array({
1024-
'id': id,
1025-
'propertyName': fullPath,
1026-
'readOnly': propertyReadOnly,
1027-
'classId': model,
1028-
'type': 'any',
1029-
'arr': getStructureValue(model, id, fullPath)
1030-
});
1031-
break;
1032-
case $metamodel.isStructure(propertyName, model):
1033-
propertyValue = addStructure(parentPath, propertyName, model, id);
1034-
break;
1035-
default:
1036-
propertyValue = getStructureValue(model, id, fullPath);
1037-
break;
1038-
}
1039-
if (propertyValue === undefined && prop.default !== undefined) {
1040-
propertyValue = prop.default;
1041-
}
1042-
return propertyValue;
1043-
} else {
1044-
$log.destroyedComponentCall(fullPath, id);
1010+
function _isValidCollection(coll, type) {
1011+
1012+
var result = true;
1013+
coll.forEach(function (val) {
1014+
if (!$metamodel.isValidType(val, type)) {
1015+
result = result && false;
1016+
}
1017+
});
1018+
1019+
return true;
10451020
}
1046-
} else {
1047-
if (propertyReadOnly) {
1048-
$log.readOnlyProperty(id, model, fullPath);
1021+
1022+
if (path) {
1023+
parentPath = parentPath + '.' + name;
10491024
} else {
1050-
if ($metamodel.isValidType(value, propertyType)) {
1051-
search = $db[model].find({ '_id': id });
1052-
if (search.length) {
1053-
component = search[0];
1054-
1055-
switch (true) {
1056-
case propertyType.indexOf('@') !== -1:
1057-
setStructureValue(model, id, fullPath, value.id());
1058-
break;
1059-
case propertyType === 'date':
1060-
setStructureValue(model, id, fullPath, value.toISOString());
1061-
break;
1062-
default:
1063-
setStructureValue(model, id, fullPath, value);
1064-
break;
1065-
}
1025+
parentPath = name;
1026+
}
1027+
fullPath = parentPath + '.' + propertyName;
1028+
1029+
if (typeof value === 'undefined') {
1030+
if (typeof position === 'undefined') {
1031+
1032+
runtimeArr = new _Array({
1033+
'id': id,
1034+
'propertyName': fullPath,
1035+
'readOnly': propertyReadOnly,
1036+
'classId': model,
1037+
'type': 'any',
1038+
'arr': getStructureValue(model, id, fullPath)
1039+
});
10661040

1067-
if ($helper.isRuntime() && $helper.getRuntime().require('db')) {
1068-
$helper.getRuntime().require('db').update({
1069-
'collection': model,
1070-
'id': id,
1071-
'field': fullPath,
1072-
'value': value
1041+
return runtimeArr;
1042+
} else {
1043+
if (Array.isArray(position)) { // we replace the collection
1044+
if (_isValidCollection(position, 'any')) {
1045+
search = $db[model].find({
1046+
'_id': id
10731047
});
1048+
if (search.length) {
1049+
1050+
setStructureValue(model, id, fullPath, position);
1051+
1052+
$workflow.state({
1053+
'component': id,
1054+
'state': fullPath,
1055+
'data': [position, 'reset']
1056+
});
1057+
1058+
if ($helper.isRuntime()) {
1059+
$helper.getRuntime().require('db').update({
1060+
'collection': model,
1061+
'id': id,
1062+
'field': fullPath,
1063+
'value': position
1064+
});
1065+
}
1066+
}
1067+
} else {
1068+
$log.invalidPropertyName(id, this.constructor.name, propertyName, position, propertyType);
1069+
}
1070+
} else {
1071+
if (typeof position === 'number') {
1072+
val = getStructureValue(model, id, fullPath)[position];
1073+
return val;
1074+
} else {
1075+
$log.invalidPropertyName(id, this.constructor.name, propertyName, position, 'number');
10741076
}
1077+
}
1078+
}
1079+
} else {
1080+
if (propertyReadOnly) {
1081+
$log.readOnlyProperty(id, this.constructor.name, propertyName);
1082+
} else {
1083+
if ($metamodel.isValidType(value, 'any')) {
1084+
search = $db[model].find({
1085+
'_id': id
1086+
});
1087+
if (search.length) {
10751088

1076-
// case of _Behavior
1077-
if (model === '_Behavior') {
1078-
$behavior.removeFromMemory(id);
1089+
var arr = getStructureValue(model, id, fullPath);
1090+
if (typeof arr === 'undefined') {
1091+
arr = [];
1092+
}
1093+
arr[position] = value;
1094+
setStructureValue(model, id, fullPath, arr);
1095+
1096+
if ($helper.isRuntime()) {
1097+
$helper.getRuntime().require('db').update({
1098+
'collection': model,
1099+
'id': id,
1100+
'field': fullPath,
1101+
'value': arr
1102+
});
1103+
}
1104+
1105+
$workflow.state({
1106+
'component': id,
1107+
'state': fullPath,
1108+
'data': [arr, 'add']
1109+
});
10791110
}
1111+
} else {
1112+
$log.invalidPropertyName(id, this.constructor.name, propertyName, value, propertyType);
1113+
}
1114+
}
1115+
}
1116+
};
10801117

1081-
$workflow.state({
1082-
'component': id,
1083-
'state': fullPath,
1084-
'data': [value]
1085-
});
1118+
/* jshint -W054 */
1119+
sructure[propertyName] = new Function('__body', 'return function ' + propertyName + ' (position,value) { return __body.call(this, position, value) };')(body);
1120+
/* jshint +W054 */
1121+
} else {
1122+
body = function body(value) {
1123+
var search = [],
1124+
component = null,
1125+
propertyValue = null,
1126+
parentPath = '',
1127+
fullPath = '';
1128+
1129+
if (path) {
1130+
parentPath = parentPath + '.' + name;
1131+
} else {
1132+
parentPath = name;
1133+
}
1134+
fullPath = parentPath + '.' + propertyName;
1135+
1136+
if (typeof value === 'undefined') {
1137+
component = $db.store[model][id];
1138+
if (component) {
1139+
switch (true) {
1140+
case propertyType.indexOf('@') !== -1:
1141+
propertyValue = get(getStructureValue(model, id, fullPath));
1142+
break;
1143+
case propertyType === 'date':
1144+
propertyValue = new Date(getStructureValue(model, id, fullPath));
1145+
break;
1146+
case $metamodel.isStructure(propertyName, model):
1147+
propertyValue = addStructure(parentPath, propertyName, model, id);
1148+
break;
1149+
default:
1150+
propertyValue = getStructureValue(model, id, fullPath);
1151+
break;
10861152
}
1153+
if (propertyValue === undefined && prop.default !== undefined) {
1154+
propertyValue = prop.default;
1155+
}
1156+
return propertyValue;
10871157
} else {
1088-
$log.invalidPropertyName(id, model, fullPath, value, propertyType);
1158+
$log.destroyedComponentCall(fullPath, id);
10891159
}
1090-
}
1091-
}
1092-
};
1160+
} else {
1161+
if (propertyReadOnly) {
1162+
$log.readOnlyProperty(id, model, fullPath);
1163+
} else {
1164+
if ($metamodel.isValidType(value, propertyType)) {
1165+
search = $db[model].find({ '_id': id });
1166+
if (search.length) {
1167+
component = search[0];
10931168

1094-
/* jshint -W054 */
1095-
sructure[propertyName] = new Function('__body', 'return function ' + propertyName + ' (value) { return __body.call(this,value) };')(body);
1096-
/* jshint +W054 */
1169+
switch (true) {
1170+
case propertyType.indexOf('@') !== -1:
1171+
setStructureValue(model, id, fullPath, value.id());
1172+
break;
1173+
case propertyType === 'date':
1174+
setStructureValue(model, id, fullPath, value.toISOString());
1175+
break;
1176+
default:
1177+
setStructureValue(model, id, fullPath, value);
1178+
break;
1179+
}
1180+
1181+
if ($helper.isRuntime() && $helper.getRuntime().require('db')) {
1182+
$helper.getRuntime().require('db').update({
1183+
'collection': model,
1184+
'id': id,
1185+
'field': fullPath,
1186+
'value': value
1187+
});
1188+
}
1189+
1190+
// case of _Behavior
1191+
if (model === '_Behavior') {
1192+
$behavior.removeFromMemory(id);
1193+
}
1194+
1195+
$workflow.state({
1196+
'component': id,
1197+
'state': fullPath,
1198+
'data': [value]
1199+
});
1200+
}
1201+
} else {
1202+
$log.invalidPropertyName(id, model, fullPath, value, propertyType);
1203+
}
1204+
}
1205+
}
1206+
};
1207+
1208+
/* jshint -W054 */
1209+
sructure[propertyName] = new Function('__body', 'return function ' + propertyName + ' (value) { return __body.call(this,value) };')(body);
1210+
/* jshint +W054 */
1211+
}
10971212
});
10981213

10991214
return sructure;

0 commit comments

Comments
 (0)