Web App Mvc

4,528 views

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
4,528
On SlideShare
0
From Embeds
0
Number of Embeds
2,520
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Web App Mvc

  1. 1. Web App MVC Jit Im Will 小问
  2. 2. Front + Back
  3. 3. JavaScript MVC + Node.js MVC
  4. 4. Front
  5. 5. jQuery MooTools YUI Prototype ExtJS Dojo
  6. 6. Mustache.js Underscore.js Backbone.js Socket.io jQuery
  7. 7. Back
  8. 8. ExpressJS Web.js Connect
  9. 9. Underscore.js Socket.io Backbone.js JSData.js MongoSkin Web.js
  10. 10. 前后端 MVC 交互 传统 /*PHP*/ $result = mysql_query ( 'SELECT name FROM users' ); while ( $row = mysql_fetch_array ( $result )) { echo &quot; <b> &quot; . $row [ 'name' ]. &quot; </b> &quot; ; echo &quot; <br/> &quot; ; } //jQuery $ . get ( location . origin + '/names.php' ) . done (function ( data ) { $ ( 'body' ). append ( data ); });
  11. 11. <ul><li>服务器运算延迟! </li></ul><ul><li>HTTP 传输性能有限! </li></ul><ul><li>终端设备性能大大浪费! </li></ul>
  12. 12. 高性能 //Node.js with Web.js and MongoSkin var getRouter = { 'names' : function ( req , res , qs ) { db . collection ( 'names' ) . find ({}) . toArray (function ( err , names ) { res . sendJSON ( names ); }) } }; web . get ( getRouter ); 前后端 MVC 交互
  13. 13. 前后端 MVC 交互 高性能 //jQuery, Mustache.js $ . get ( location . origin + ' /names ' ) . done (function ( data ) { var $names = JSON . parse (data); $ . get ( location . origin + ' /tmlp/names.html ' ) . done (function ( data ) { $html = Mustache . to_html (data, {names: $names }); $( ' body ' ) . append ( $html ); }); });
  14. 15. 终端性能 <ul><li>CPU 运算 </li></ul><ul><ul><li>V8 来自 Google 的基于 C++ 的高性能 JavaScript 引擎 </li></ul></ul><ul><ul><li>Worker API 多线程 JavaScript 运行机制 </li></ul></ul><ul><li>GPU 硬件加速 </li></ul><ul><ul><li>Canvas 绘图加速 </li></ul></ul><ul><ul><li>Video API 解码加速 </li></ul></ul><ul><ul><li>DOM 渲染加速 </li></ul></ul><ul><li>移动终端的超强交互性能 </li></ul>
  15. 16. 终端性能 <ul><li>存储机制 </li></ul><ul><ul><li>Cookies </li></ul></ul><ul><ul><li>localStorage </li></ul></ul><ul><ul><li>sessionStorage </li></ul></ul><ul><ul><li>Web SQL </li></ul></ul><ul><ul><li>Cache </li></ul></ul><ul><li>交互机制 </li></ul><ul><ul><li>WebSocket </li></ul></ul><ul><ul><li>Message Model </li></ul></ul><ul><ul><li>Ajax Level 2 </li></ul></ul><ul><ul><li>…… </li></ul></ul>不仅不能避免缓存, 而且要主动存储! 不要让 Web 成为 Long Polling 的天下!
  16. 17. 服务端资源 ¥ 5800 / 年 Xeon 双核, 1.5G 内存, 150G 存储空间,千兆带宽光纤
  17. 18. 服务端资源 ¥ 25500 元 / 年! Xeon 八核, 16G 内存, 750G 存储空间,千兆带宽光纤
  18. 19. 服务器资源 <ul><li>HTTP 资源 </li></ul><ul><li>TCP 资源 </li></ul><ul><li>LAN 资源 </li></ul><ul><li>存储资源 </li></ul><ul><li>内存资源 </li></ul><ul><li>数据库资源 </li></ul><ul><li>CPU 资源 </li></ul><ul><li>…… </li></ul>
  19. 20. NoSQL 垂直查询 异步通信 关系链 线程运算 垂直查询 关系链 异步通信 NoSQL NoSQL 关系链
  20. 21. 前后端优化 Quick Simple Light MVC 高速架构 NoSQL 数据模型 JSON 轻量传输
  21. 22. JavaScript — 因为不严谨而飞速发展的脚本语言 Node.js — 基于不严谨的强悍服务器 Web.js — 基于简易的高性能 Node.js 开发框架
  22. 23. require ( 'webjs' ). run () Simple Deployment
  23. 24. File System
  24. 25. //ExpressJS var app = require ( 'express' ). createServer (); app . get ( '/*' , function ( req , res ) { res . sendfile ( req . get ( 'content-disposition' , 'filename' )); }); app . listen ( 80 ); //Web.js require ( 'webjs' ). run () //Node.js var http = require ( 'http' ), fs = require ( 'fs' );……… server . on ( 'request' , function ( req , res ) { ……… }). listen ( 80 );
  25. 26. Node.js MVC
  26. 27. M Model
  27. 29. var Person = Backbone.Model.extend({ sayHello : function () { console.log(this.get('name')); } }); var me = new Person({name: 'Will'}); me.sayHello(); // -> 'Will'
  28. 30. var People = Backbone.Collection.extend({ sayHello : function () { this.map(function (person) { return this.get('name') + 'say: &quot;Hello, I`m ' + this.get('name') + '.&quot;n'; }); } }); var w3ctech = new People; w3ctech.add({name: 'Will'}, {name: 'Foo'}, {name: 'Bar'}); w3ctech.sayHello(); /** -> Will say: &quot;Hello, I`m Will.&quot; * Foo say: &quot;Hello, I`m Foo.&quot; * Bar say: &quot;Hello, I`m Bar.&quot; **/
  29. 31. V View
  30. 32. { Mustache.js
  31. 33. Mustache.js < table > < tbody > {{# peoples }} {{# Man }} < tr > < td >{{ Name }}</ td > < td >{{ Age }}</ td > < td >{{ Sex }}</ td > </ tr > {{/ Man }} {{/ peoples }} </ tbody > </ table > 模版
  32. 34. Mustache.js var obj = { peoples : [ { Name : 'Will' , Age : 15 , Sex : 'Man' }, { Name : 'Foo' , Age : 30 , Sex : 'Woman' }, { Name : 'Bar' , Age : 18 , Sex : 'Man' }], Man : function () { if ( this . Sex == 'Man' ) { return function ( text , render ) { render ( text ); } } } }; 对象
  33. 35. Web.js with Mustache.js <ul><li>var getRouter = { </li></ul><ul><li>'^mans' : function ( req , res , qs ) { </li></ul><ul><li>mansHtml = web . render ( 'mans' , obj ); </li></ul><ul><li>res . send ( mansHtml ); </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>web . set ( 'tmplDir' , 'tmlps' ) </li></ul><ul><li>. get ( getRouter ); </li></ul>
  34. 36. C Controller
  35. 37. Web.js <ul><li>var postRouter = { </li></ul><ul><li>'seturlrouter' : function ( req , res , qs ){ </li></ul><ul><ul><li>web . get ({ qs . key : qs . value }); </li></ul></ul><ul><ul><li>res . send ( 'Set success' ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>}; </li></ul><ul><li>web . post ( postRouter ); </li></ul>
  36. 38. JavaScript MVC
  37. 39. <ul><li>//For IE </li></ul><ul><li>function parseQs () { </li></ul><ul><li>var qs = ( location . search > 0 ? location . search . substring ( 1 ) : '' ), </li></ul><ul><li>params = {}, </li></ul><ul><li>items = qs . split ( &quot;&&quot; ), </li></ul><ul><li>_item = null ; </li></ul><ul><li>for (var i = 0 ; i < items . length ; i ++) { </li></ul><ul><li>item = items [ i ]; </li></ul><ul><li>params [ decodeURIComponent ( item [ 0 ])] = decodeURIComponent ( item [ 1 ]); </li></ul><ul><li>} </li></ul><ul><li>return params ; </li></ul><ul><li>} </li></ul><ul><li>location . qs = parseOs (); </li></ul>JavaScript MVC
  38. 40. JavaScript MVC //For Chrome, FireFox and more location . __defineGetter__ ( 'qs' , function() { var qs = ( location . search > 0 ? location . search . substring ( 1 ) : '' ), params = {}, items = qs . split ( &quot;&&quot; ), _item = null ; for (var i = 0 ; i < items . length ; i ++) { item = items [ i ]; params [ decodeURIComponent ( item [ 0 ])] = decodeURIComponent ( item [ 1 ]); } return params ; });
  39. 41. JavaScript MVC (function ( $ ) { $ . getScript ( location . origin + '/js/web-client.js' ); //Web.js for Client var getRouter = { 'getsomething' : function () { $ ( 'body' ). append ( web . getData ( 'something' ) . render ( 'someTmpl' ) ); } }; web . conn ( location . origin ) . get ( getRouter ) })( jQuery );
  40. 42. Page JavaScript MVC Node.js 3.Data Trans 2.Page Action Router 1.Server Router
  41. 43. http://www.iwillwen.com https://github.com/iwillwen/Web.js
  42. 44. Q&A

×