Top Banner
Web App MVC Jit Im Will 小小
44

Web App Mvc

Apr 22, 2015

Download

Technology

Will Gunn

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Web App Mvc

Web App MVC

Jit Im Will 小问

Page 2: Web App Mvc

Front

+

Back

Page 3: Web App Mvc

JavaScript MVC+

Node.js MVC

Page 4: Web App Mvc

Front

Page 5: Web App Mvc

jQuery

MooTools

YUI

Prototype

ExtJS

Dojo

Page 6: Web App Mvc

Mustache.js

Underscore.js

Backbone.js

Socket.iojQuery

Page 7: Web App Mvc

Back

Page 8: Web App Mvc

ExpressJS

Web.js

Connect

Page 9: Web App Mvc

Underscore.js

Socket.io Backbone.js

JSData.jsMongoSkin

Web.js

Page 10: Web App Mvc

前后端 MVC 交互传统/*PHP*/$result = mysql_query('SELECT name FROM users');while ($row = mysql_fetch_array($result)) { echo "<b>".$row['name']."</b>"; echo "<br/>";}

//jQuery$.get(location.origin + '/names.php') .done(function (data) { $('body').append(data); });

Page 11: Web App Mvc

•服务器运算延迟!•HTTP 传输性能有限!•终端设备性能大大浪费!

Page 12: Web App Mvc

高性能//Node.js with Web.js and MongoSkinvar getRouter = {

'names' : function (req, res, qs) {db.collection('names') .find({}) .toArray(function (err, names) {

res.sendJSON(names); })

}};

web.get(getRouter);

前后端 MVC 交互

Page 13: Web App Mvc

前后端 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); }); });

Page 14: Web App Mvc
Page 15: Web App Mvc

终端性能• CPU 运算

– V8 来自 Google 的基于 C++ 的高性能 JavaScript 引擎

– Worker API 多线程 JavaScript 运行机制• GPU 硬件加速

– Canvas 绘图加速– Video API 解码加速– DOM 渲染加速

• 移动终端的超强交互性能

Page 16: Web App Mvc

终端性能• 存储机制

– Cookies– localStorage– sessionStorage– Web SQL– Cache

• 交互机制– WebSocket– Message Model– Ajax Level 2– ……

不仅不能避免缓存,

而且要主动存储!

不要让 Web 成为

Long Polling 的天下!

Page 17: Web App Mvc

服务端资源

¥ 5800 / 年

Xeon 双核, 1.5G 内存, 150G 存储空间,千兆带宽光纤

Page 18: Web App Mvc

服务端资源

¥ 25500 元 / 年!

Xeon 八核, 16G 内存, 750G 存储空间,千兆带宽光纤

Page 19: Web App Mvc

服务器资源• HTTP 资源• TCP 资源• LAN 资源• 存储资源• 内存资源• 数据库资源• CPU 资源• ……

Page 20: Web App Mvc

NoSQL

垂直查询 异步通信关系链

线程运算垂直查询关系链

异步通信

NoSQL

NoSQL

关系链

Page 21: Web App Mvc

前后端优化

Quick Simple Light MVC

高速架构

NoSQL

数据模型

JSON

轻量传输

Page 22: Web App Mvc

JavaScript — 因为不严谨而飞速发展的脚本语言

Node.js — 基于不严谨的强悍服务器

Web.js — 基于简易的高性能Node.js 开发框架

Page 23: Web App Mvc

require('webjs').run()

Simple Deployment

Page 24: Web App Mvc

File System

Page 25: Web App Mvc

//ExpressJSvar app = require('express').createServer();app.get('/*', function (req, res) {

res.sendfile(req.get('content-disposition', 'filename'));});app.listen(80);

//Web.jsrequire('webjs').run()

//Node.jsvar http = require('http'), fs = require('fs');………server.on('request', function (req, res) {………}).listen(80);

Page 26: Web App Mvc

Node.js MVC

Page 27: Web App Mvc

MModel

Page 28: Web App Mvc
Page 29: Web App Mvc

var Person = Backbone.Model.extend({ sayHello : function () { console.log(this.get('name')); }});

var me = new Person({name: 'Will'});

me.sayHello(); // -> 'Will'

Page 30: Web App Mvc

var People = Backbone.Collection.extend({ sayHello : function () { this.map(function (person) { return this.get('name') + 'say: "Hello, I`m ' + this.get('name') + '."\n'; }); }});var w3ctech = new People;w3ctech.add({name: 'Will'}, {name: 'Foo'}, {name: 'Bar'});w3ctech.sayHello(); /** -> Will say: "Hello, I`m Will."

* Foo say: "Hello, I`m Foo." * Bar say: "Hello, I`m Bar." **/

Page 31: Web App Mvc

VView

Page 32: Web App Mvc

{Mustache.js

Page 33: Web App Mvc

Mustache.js

<table> <tbody> {{#peoples}} {{#Man}} <tr> <td>{{Name}}</td> <td>{{Age}}</td> <td>{{Sex}}</td> </tr> {{/Man}} {{/peoples}} </tbody></table>

模版

Page 34: Web App Mvc

Mustache.jsvar 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);

}}

}};

对象

Page 35: Web App Mvc

Web.js with Mustache.js

var getRouter = { '^mans' : function (req, res, qs) { mansHtml = web.render('mans', obj);

res.send(mansHtml);}

};web.set('tmplDir', 'tmlps') .get(getRouter);

Page 36: Web App Mvc

CController

Page 37: Web App Mvc

Web.js

var postRouter = {

'seturlrouter': function (req, res, qs){

web.get({qs.key : qs.value});

res.send('Set success');

}

};

web.post(postRouter);

Page 38: Web App Mvc

JavaScript MVC

Page 39: Web App Mvc

//For IE

function parseQs() {

var qs = (location.search > 0 ? location.search.substring(1) : ''),

params = {},

items = qs.split("&"),

_item = null;

for (var i = 0; i < items.length; i++) {

item = items[i];

params[decodeURIComponent(item[0])] = decodeURIComponent(item[1]);

}

return params;

}

location.qs = parseOs();

JavaScript MVC

Page 40: Web App Mvc

JavaScript MVC

//For Chrome, FireFox and more

location.__defineGetter__('qs', function() {

var qs = (location.search > 0 ? location.search.substring(1) : ''),

params = {},

items = qs.split("&"),

_item = null;

for (var i = 0; i < items.length; i++) {

item = items[i];

params[decodeURIComponent(item[0])] = decodeURIComponent(item[1]);

}

return params;

});

Page 41: Web App Mvc

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);

Page 42: Web App Mvc

Page

JavaScript MVC

Node.js

3.Data Trans

2.Page Action Router

1.Server Router

Page 44: Web App Mvc

Q&A