GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — js_designpatterns/mvc.js at master · m1oto/js_designpatterns Expand file tree/ Copy pathmvc.jsMore file actions 71 lines (66 loc) · 1.78 KB / Copy pathmvc.jsFile metadata and controls71 lines (66 loc) · 1.78 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 // 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.accountView(); } this.discount = function(amount) { this.setBalance(model.getBalance() - amount); this.accountView(); } 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 |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |