-
Notifications
You must be signed in to change notification settings - Fork 1
/
mvc.js
69 lines (64 loc) · 1.74 KB
/
mvc.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
// Perhaps the most used pattern in javascript web development.
// MVC(Model-View-Controller) seperates data model representation
// from data visuallization.
// The glue that connects the model changes and view updates
// is the contoller.
// Define Account Model
function AccountModel(owner, balance) {
this.owner = owner;
this.balance = balance;
}
AccountModel.prototype = {
getOwner: function() {
return this.owner;
},
getBalance: function() {
return this.balance;
}
}
// Define Account View
function AccountView() {}
AccountView.prototype.showAccountDetails = function(owner, balance) {
console.log('Account owner: ' + owner);
console.log('Account balance: ' + balance);
}
// Define Account Controller
function AccountController(model, view) {
this.accountView = function() {
view.showAccountDetails(model.getOwner(), model.getBalance());
}
this.deposit = function(amount) {
this.setBalance(model.getBalance() + amount);
}
this.discount = function(amount) {
this.setBalance(model.getBalance() - amount);
}
this.setOwner = function(newOwner) {
model.owner = newOwner;
this.accountView();
}
this.setBalance = function(newBalance) {
model.balance = newBalance;
this.accountView();
}
}
// Usage Example
var controller = new AccountController(new AccountModel('Ben', 15000), new AccountView());
controller.accountView();
// Account owner: Ben
// Account balance: 15000
controller.deposit(500);
// Account owner: Ben
// Account balance: 15500
controller.discount(120);
// Account owner: Ben
// Account balance: 15380
controller.setOwner('Bob');
// Account owner: Bob
// Account balance: 15380
controller.setBalance(0);
// Account owner: Bob
// Account balance: 0
controller.deposit(1000000);
// Account owner: Bob
// Account balance: 1000000