-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcustomer.html
More file actions
90 lines (78 loc) · 2 KB
/
Copy pathcustomer.html
File metadata and controls
90 lines (78 loc) · 2 KB
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
<!DOCTYPE html>
<html>
<head>
<link href="prism.css" rel="stylesheet" />
<link href="index.css" rel="stylesheet" />
<script src="../../dist/supermodels.js"></script>
<script src="examples.js"></script>
<script class="code">
var BasketItem = supermodels({
productCode: String,
quantity: {
__type: Number,
__validators: [function(value) {
if (!value) {
return 'Quantity is required';
}
}]
}
}, function(productCode, quantity) {
this.productCode = productCode;
this.quantity = quantity;
});
var Basket = supermodels({
items: {
__type: [BasketItem],
__validators: [function() {
if (!this.length) {
return 'A least one basket item is required';
}
}]
},
total: function() {
return this.items.length;
},
totalItems: function() {
var total = 0;
for (var i = 0; i < this.items.length; i++) {
total += this.items[i].quantity;
}
return total;
},
__validators: [function(value) {
if (this.totalItems() > 5) {
return 'Maximum total quantity of all items is 5';
}
}]
});
var Customer = supermodels({
name: String,
basket: {
__type: Basket,
__validators: [function(value, key) {
if (!value) {
console.log(key);
return 'Basket is required';
}
}]
}
});
var customer = new Customer();
console.log(customer.errors);
customer.basket = new Basket();
console.log(customer.errors);
customer.basket.items.push(new BasketItem());
console.log(customer.errors);
customer.basket.items[0].productCode = 'P1';
customer.basket.items[0].quantity = 3;
console.log(customer.errors);
customer.basket.items.push(new BasketItem('P2', 3));
console.log(customer.errors);
</script>
</head>
<body>
<pre class="language-javascript"><code class="language-javascript"></code></pre>
<script src="index.js"></script>
<script src="prism.js"></script>
</body>
</html>