-
Notifications
You must be signed in to change notification settings - Fork 4
/
data-model-view.js
86 lines (82 loc) · 3.21 KB
/
data-model-view.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
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved
var _ = require('lodash');
var {DataField} = require('./types');
/**
* @class DataModelView
* @property {string} title - Gets or sets the title of the current view
* @property {string} name - Gets or sets the name of the current data view
* @property {boolean} public - Gets or sets a boolean that indicates whether this data view is public or not.The default value is true.
* @property {boolean} sealed - Gets or sets a boolean that indicates whether this data view is sealed or not. The default value is true.
* @property {string|QueryExpression|*} filter - Gets or sets an open data formatted filter string or a query expression object associated with this view.
* @property {string|*} order - Gets or sets an open data formatted order string or an order expression object associated with this view.
* @property {string|*} group - Gets or sets an open data formatted group string or a group expression object associated with this view.
* @property {Array} fields - Gets or sets the collection of data view's fields
* @property {DataModel} model - Gets a DataModel instance that represents the parent model of the current view
* @property {Array} attributes - A readonly collection of DataField instances
* @param {DataModel} model - The parent model associated with this view
* @constructor
*/
function DataModelView(model) {
this.public = true;
this.sealed = true;
this.fields = [];
var _model = model;
Object.defineProperty(this,'model', {
get: function() {
return _model;
}, configurable:false, enumerable: false
});
var self = this;
Object.defineProperty(this,'attributes', {
get: function() {
var attrs = [];
self.fields.forEach(function(x) {
if (self.model) {
var field = _.assign(new DataField(), self.model.field(x.name));
if (field)
attrs.push(_.assign(field, x));
else
attrs.push(_.assign({}, x));
}
else
//unbound view (?)
attrs.push(_.assign({}, x));
});
return attrs;
}, configurable:false, enumerable: false
});
}
/**
* Casts an object or an array of objects based on view's field collection.
* @param {Array|*} obj
* @returns {Array|*}
*/
DataModelView.prototype.cast = function(obj) {
var self = this, res;
var localFields = this.fields.filter(function(y) {
return !_.isNil(self.model.field(y.name));
});
if (_.isArray(obj)) {
var arr = [];
obj.forEach(function(x) {
res = {};
localFields.forEach(function(y) {
if (typeof x[y.name] !== 'undefined')
res[y.name] = x[y.name];
});
arr.push(res);
});
return arr;
}
else {
res = { };
localFields.forEach(function(y) {
if (typeof obj[y.name] !== 'undefined')
res[y.name] = obj[y.name];
});
return res;
}
};
module.exports = {
DataModelView
}