5. MOSKY
• Python Charmer at Pinkoi
• An author of some Python packages
• A speaker of some confs, PyCon TW/JP mostly
2
6. MOSKY
• Python Charmer at Pinkoi
• An author of some Python packages
• A speaker of some confs, PyCon TW/JP mostly
• A Python instructor
2
7. MOSKY
• Python Charmer at Pinkoi
• An author of some Python packages
• A speaker of some confs, PyCon TW/JP mostly
• A Python instructor
• mosky.tw
2
62. THE RECIPE
• Use Class in JavaScript;
• Write 3 methods as Model, View, and Controller
16
63. THE RECIPE
• Use Class in JavaScript;
• Write 3 methods as Model, View, and Controller
• Make function call as message passing
16
64. THE RECIPE
• Use Class in JavaScript;
• Write 3 methods as Model, View, and Controller
• Make function call as message passing
• Message is what changed.
16
65. THE RECIPE
• Use Class in JavaScript;
• Write 3 methods as Model, View, and Controller
• Make function call as message passing
• Message is what changed.
• And the util-level libs you love.
16
66. THE RECIPE
• Use Class in JavaScript;
• Write 3 methods as Model, View, and Controller
• Make function call as message passing
• Message is what changed.
• And the util-level libs you love.
• (My favor is just JQuery and Underscore.js)
16
67. THE CONSTRUCTOR
var Clock = function (obj) {
!
/* Model */
this._model = {};
!
/* View */
this.$view = $(Clock.template);
this.$i = $this.view.find('.i');
this.$o = $this.view.find('.o');
// ...
!
!
/* Controller */
var _this = this;
this.$view.on('change', '.i',
function () {
_this.controller('i-changed');
});
// ...
!
// Apply defaults
this.model(obj);
};
17
68. THE MODEL
Clock.prototype.model = function (model_changes) {
!
// First, optionally filter the fake changes out.
!
// Second, asyncly send the changes to server;
// and update model/view by the response.
!
// Finally, update the changes into this._model.
!
this.view(view_changes);
};
18
69. THE VIEW
Clock.prototype.view = function (view_changes) {
!
// Pattern I
if (view_changes.o !== undefined) {
this.$o.val(view_changes.o);
}
!
// Pattern II
this.$n.val(this._model.o);
};
19
74. REAL CASES
• The Memo App
• https://github.com/moskytw/memo-app/blob/
master/memo/static/memo.js
22
75. REAL CASES
• The Memo App
• https://github.com/moskytw/memo-app/blob/
master/memo/static/memo.js
• The Web for ZIPCodeTW
22
76. REAL CASES
• The Memo App
• https://github.com/moskytw/memo-app/blob/
master/memo/static/memo.js
• The Web for ZIPCodeTW
• https://github.com/moskytw/zipcodetw/blob/
dev/web/static/finder.js
22
77. REAL CASES
• The Memo App
• https://github.com/moskytw/memo-app/blob/
master/memo/static/memo.js
• The Web for ZIPCodeTW
• https://github.com/moskytw/zipcodetw/blob/
dev/web/static/finder.js
• http://zipcode.mosky.tw/
22
86. CONCLUSION
• MVC is a powerful pattern, and not hard.
• Add model unit to simplify problem.
• Don’t be limited by the frameworks!
27
87. CONCLUSION
• MVC is a powerful pattern, and not hard.
• Add model unit to simplify problem.
• Don’t be limited by the frameworks!
• mosky.tw
27
88. CONCLUSION
• MVC is a powerful pattern, and not hard.
• Add model unit to simplify problem.
• Don’t be limited by the frameworks!
• mosky.tw
• Any questions?
27