-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
189 lines (147 loc) · 5.72 KB
/
index.html
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
"use strict";
var Entity = function() {}
Object.defineProperty(Entity.prototype, '$values', {
value: {}
});
Object.defineProperty(Entity.prototype, '$dirty', {
value: false,
writable: true
});
var projectSchema = new ProjectSchema();
var Project = function() {}
Project.prototype = Entity.prototype;
Project.prototype.$schema = projectSchema;
var RRM = function() {
var self = this;
var defineProperty = function(entity, name, property, data) {
if (!('transform' in property)) {
throw Error('transform is mandatory');
}
self.loadPropertyValue(entity, name, property, data);
var definition = {};
if ('get' in property) {
definition.get = function() {
return property.get.call(entity);
}
}
if ('set' in property) {
definition.set = function(value) {
entity.$dirty = true;
return property.set.call(entity, value);
}
}
Object.defineProperty(entity, name, definition);
};
this.loadPropertyValue = function(entity, name, property, data) {
var value = property.transform(data);
entity.$values[name] = value;
}
this.create = function(entityClass, data) {
var entity = new entityClass;
for (var i in entity.$schema) {
defineProperty(entity, i, entity.$schema[i], data);
}
return entity;
};
this.save = function(entity) {
var data = {};
for (var i in entity.$schema) {
var property = entity.$schema[i];
if (!('reverseTransform' in property)) {
continue;
}
property.reverseTransform.call(entity, data);
}
entity.$dirty = false;
console.log(data);
repo.update(entity, data);
};
}
var rrm = new RRM;
var ResourceArray = function( repository ) {
this.filter = function(){}
this.get = function(){}
}
var ProjectRepository = function(rrm) {
var instancesById = {};
var instances = [];
this.addToIdentityMap = function(entity) {
if (entity.id !== undefined) {
if (entity.id in instancesById) {
throw Error('Entity was already loaded');
}
instancesById[entity.id] = entity;
}
if (instances.indexOf(entity) !== -1) {
return;
}
instances.push(entity);
}
this.removeFromIdentityMap = function(entity) {
var index = instances.indexOf(entity);
if (-1 === index) {
return;
}
instances.splice(index, 1);
if (entity.id !== undefined) {
delete instancesById[entity.id];
}
}
this.update = function(entity, data) {
for (var i in entity.$values) {
delete entity.$values[i];
}
for (var i in entity.$schema) {
rrm.loadPropertyValue(entity, i, entity.$schema[i], data);
}
}
this.initialize = function(data) {
var entity = rrm.create(Project, data);
try {
this.addToIdentityMap(entity);
} catch (e) {
this.update(instancesById[entity.id], data);
entity = instancesById[entity.id];
}
return entity;
}
this.getPromise = function(id) {
var deferred = $q.defer();
return deferred.promise;
}
this.get = function(id) {
if (id in instancesById) {
return instancesById[id];
}
// it is loaded from the server
return this.initialize({ id: 2, name: 'hi', description: 'bye', created_at: '2008-05-16T00:00:00Z' });
}
}
var repo = new ProjectRepository(rrm);
var project = repo.initialize({ name: 'Project', description: 'This is the description', created_at: '2010-05-16T00:00:00Z' });
console.log("id: " + project.id + ", name: " + project.name + ', description: '+ project.description +', age:' + project.age);
console.log(project.$dirty);
try {
project.id = 2;
} catch (e) {
} finally {
console.log(project.$dirty);
}
project.name = "other project";
console.log(project.$dirty);
rrm.save(project);
console.log(project.$dirty);
repo.update(project, { id: 1, name: 'blep', description: 'herp', created_at: '2010-11-16T00:00:00Z' });
console.log("id: " + project.id + ", name: " + project.name + ', description: '+ project.description +', age:' + project.age);
var project2 = repo.get(2);
console.log("id: " + project2.id + ", name: " + project2.name + ', description: '+ project2.description +', age:' + project2.age);
</script>
</head>
<body>
</body>
</html>