Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
阿里旅行去啊H5首页总结&Promise
Search
ningzbruc
August 06, 2015
Programming
270
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
阿里旅行去啊H5首页总结&Promise
阿里旅行去啊H5首页总结&Promise
ningzbruc
August 06, 2015
More Decks by ningzbruc
See All by ningzbruc
如何写出一个优秀的开源库
ningzbruc
0
68
去啊无线前端的一天
ningzbruc
1
200
React & Component
ningzbruc
0
47
KISSY.Base - all about that Base
ningzbruc
0
140
Hammer.js
ningzbruc
1
340
淘宝旅行门票iPad版开发
ningzbruc
0
140
Travel on KISSY mini
ningzbruc
0
210
Events
ningzbruc
2
130
Why YUI3
ningzbruc
0
190
Other Decks in Programming
See All in Programming
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
390
Security issues being discussed on Web Platforms
petamoriken
0
920
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
270
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
330
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
200
iOSDC2026登壇資料.pdf
riofujimon
0
150
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
160
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
110
AHC070解法紹介
eijirou
0
110
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
450
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
120
Featured
See All Featured
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
200
A Modern Web Designer's Workflow
chriscoyier
699
190k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
880
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
500
GraphQLとの向き合い方2022年版
quramy
50
15k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
450
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
320
Paper Plane
katiecoart
PRO
4
53k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Transcript
去啊H5首页总结 D[夯䔺
module KISSY.add(function(S, require, exports, module) { 'use strict'; var Q
= require('q'), IO = require('io'), UA = require('ua'), MTop = require('mtop'); module.exports = {…}; });
when not use base?
when not use base? KISSY.add(function(S, UA, Base, Util, City, Data,
View) { 'use strict'; return Base.extend([City, Data, View], {…}, { ATTRS: {…} }); }, { requires: [ 'ua', 'base', './util', './city', './data', './view' ] });
when not use base? KISSY.add(function(S, require, exports, module) { 'use
strict'; var UA = require('ua'), Util = require('./util'), City = require('./city'), Data = require('./data'), View = require('./view'); module.exports = S.merge(City, Data, View, {…}); });
when not use base? S.merge(S.Event.Target, City, Data, View, {…});
when not use base? Base is good but we should
not abuse it…
sketch 解放生产力!!!
sass ✓ rem - 字体自适应 ✓ scale - 比例缩放 ✓
1px - 高清屏细边
(function (root) { var docEl = document.documentElement; function changeRem ()
{ root.rem = docEl.getBoundingClientRect().width / 20; docEl.style.fontSize = root.rem + 'px'; } changeRem(); //... })(window, undefined); sass - rem
@function rem($values){ $list: (); @each $value in $values { $list:
#{$value/32}rem; } @return $list(); } h2 { font-size: rem(32); padding:0 rem(20); height:rem(80); line-height:rem(80); color:rgba(32,35,37,0.8); } sass - rem
sass - 1px .r1b:after { position: absolute; content: ""; top:
0; left: 0; -webkit-box-sizing: border-box; box-sizing: border-box; width: 100%; height: 100%; border: 1px solid rgba(32,35,37,0.15); } @media (-webkit-device-pixel-ratio: 2) { .r1b:after { width: 200%; height: 200%; -webkit-transform: scale(.5); transform: scale(.5); } }
sass - scale .banner { @include scaleOutter(324); } .banner-scroller {
@include scaleInner; overflow:hidden; }
@mixin scaleOutter($height, $width:640) { position:relative; padding-bottom:percentage($height / $width); height:0; overflow:visible;
} @mixin scaleInner($width:100%,$height:100%,$top:0,$left:0,$right:auto,$bottom:auto) { position:absolute; width:$width; height:$height; top:$top; left:$left; right:$right; bottom:$bottom; } sass - scale
image optimize <img data-src="${item.image|image_optim,'640x640','q75'}" class="lazyload fade" /> // 图⽚片优化 Juicer.register('image_optim',
function(url, size, q) { var suffix = '', regx = /_(\d+x\d+)?(q\d+)?\.(jpg|jpeg|png|gif)$/i, match, format; // 获取URL上的参数 if ((match = url.match(regx))) { url = url.replace(regx, ''); size = size || match[0]; q = q || match[1]; format = match[2]; } size && (suffix += size); // 尺⼨寸 q && (suffix += q); // 质量 suffix && (suffix = '_' + suffix + '.jpg'); // 后缀格式 self.isSupportWebp && (suffix += '_.webp'); // ⽀支持webp url && (url += suffix); return url; });
image optimize - webp http://gtms01.alicdn.com/tps/i1/TB1uBH9IFXXXXc.XFXXXETMFFXX-1080-547.jpeg_640x640q75.jpg_.webp use webp: 24.9kb -> 14.6kb
http://gtms01.alicdn.com/tps/i1/TB1uBH9IFXXXXc.XFXXXETMFFXX-1080-547.jpeg_640x640q75.jpg
loading render default page render getCity getData getCity getData flow
optimize 1 2
getCity getData default page / cache page getCity getData default
page / cache page diffHTML 3 4 flow optimize render render
diff html diffHTML: function(tpls, cache) { //... return { add:
add, del: del, update: update, resort: resort, rerender: add.concat(update), modify: add.length || del.length || update.length }; }, renderHTML: function(data) { // diff⼀一下各模块是否有修改 diff = this.diffHTML(tpls, ctpls); console.log(diff); // 如果有修改(增删改) if (diff.modify) { // 先销毁原先的模块逻辑 rerender = diff.rerender; this.destroyMods(rerender); // 先删除没有的模块 diff.del.forEach(function(t) { node = S.one('.' + t.id); node && node.remove(true); }); // 增加新增的模块 diff.add.forEach(function(t) { container.append(t.html); }); // 更新原有的模块 diff.update.forEach(function(t) { // !!!去抢购不需要重新替换HTML if (t.id !== 'trip_home_sale') { node = S.one('.' + t.id); node && node.replaceWith(t.html); } }); } // 如果顺序发⽣生变化 if (diff.resort) { tpls.forEach(function(t) { node = S.one('.' + t.id); node && container.append(node); }); } return rerender; },
Promise “A promise represents the eventual result of an asynchronous
operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled.” - Promises/A+ https://promisesaplus.com/
Promise getCity getData default page / cache page diffHTML render
Promise getCity getData default page / cache page diffHTML render
Promise
Promise // sync this.getCity() .getData() .render();
Promise // sync this.getCity() .getData() .render(); // async - callbacks
this.getCity(function(city) { this.getData(city, function(data) { this.render(data); }); });
Promise // sync this.getCity() .getData() .render(); // async - callbacks
this.getCity(function(city) { this.getData(city, function(data) { this.render(data); }); }); 回调过度嵌套
Promise // sync this.getCity() .getData() .render(); // async - callbacks
this.getCity(function(city) { this.getData(city, function(data) { this.render(data); }); }); // async - promise this.getCity() .then(this.getData) .then(this.render); 回调过度嵌套
Promise
Promise // async - callbacks getData: function(city, callback) { if
(yes) { callback(null, data); } else { callback(reason, null); } }
Promise // async - callbacks getData: function(city, callback) { if
(yes) { callback(null, data); } else { callback(reason, null); } } 混淆参数与返回值
Promise // async - callbacks getData: function(city, callback) { if
(yes) { callback(null, data); } else { callback(reason, null); } } // async - promise getData: function(city) { return new Promise(function(resolve, reject) { if (yes) { resolve(data); } else { reject(reason); } }); } 混淆参数与返回值
Promise
Promise // async - callbacks this.getData(city, function(err, data) { if
(err) { this.handleError(err); } else { this.render(data); } });
Promise // async - callbacks this.getData(city, function(err, data) { if
(err) { this.handleError(err); } else { this.render(data); } }); 处理错误麻烦
Promise // async - callbacks this.getData(city, function(err, data) { if
(err) { this.handleError(err); } else { this.render(data); } }); // async - promise this.getData(city) .then(this.render, this.handleError); 处理错误麻烦
Promise // async - callbacks this.getData(city, function(err, data) { if
(err) { this.handleError(err); } else { this.render(data); } }); // async - promise this.getData(city) .then(this.render, this.handleError); 处理错误麻烦 // async - promise - catch this.getData(city) .then(this.render) .catch(this.handleError);
Promise - status pending fullfilled rejected resolve reject single way
Promise - status
// async - callbacks getCity: function(callback) { var timer; if
(urlCity) { callback(urlCity); } else if (cacheCity) { callback(cacheCity); } else { this.getLocateCity(function(locateCity) { clearTimeout(timer); callback(locateCity); }); timer = setTimeout(function() { callback(defaultCity); }, 8000); } } Promise - status
// async - callbacks getCity: function(callback) { var timer; if
(urlCity) { callback(urlCity); } else if (cacheCity) { callback(cacheCity); } else { this.getLocateCity(function(locateCity) { clearTimeout(timer); callback(locateCity); }); timer = setTimeout(function() { callback(defaultCity); }, 8000); } } Promise - status 多种状态可能执行多次
// async - callbacks getCity: function(callback) { var timer; if
(urlCity) { callback(urlCity); } else if (cacheCity) { callback(cacheCity); } else { this.getLocateCity(function(locateCity) { clearTimeout(timer); callback(locateCity); }); timer = setTimeout(function() { callback(defaultCity); }, 8000); } } Promise - status // async - promise getCity: function(callback) { return new Promise(function(resolve, reject) { resolve(urlCity); resolve(cacheCity); this.getLocateCity.then(resolve); setTimeout(function() { resolve(defaultCity); }, 8000); }); } 多种状态可能执行多次
// async - callbacks getCity: function(callback) { var timer; if
(urlCity) { callback(urlCity); } else if (cacheCity) { callback(cacheCity); } else { this.getLocateCity(function(locateCity) { clearTimeout(timer); callback(locateCity); }); timer = setTimeout(function() { callback(defaultCity); }, 8000); } } Promise - status // async - promise getCity: function(callback) { return new Promise(function(resolve, reject) { resolve(urlCity); resolve(cacheCity); this.getLocateCity.then(resolve); setTimeout(function() { resolve(defaultCity); }, 8000); }); } 多种状态可能执行多次 多种状态只执行一次
Promise - all
Promise - all // async - callbacks getData: function(city, callback)
{ var count = 0, datas = []; function checkDone(data) { datas.push(data); if (++count == 2) { callback(datas); } } this.getLayoutData(city, function(data) { checkDone(data); }); this.getOnsaleData(function(data) { checkDone(data); }); }
Promise - all // async - callbacks getData: function(city, callback)
{ var count = 0, datas = []; function checkDone(data) { datas.push(data); if (++count == 2) { callback(datas); } } this.getLayoutData(city, function(data) { checkDone(data); }); this.getOnsaleData(function(data) { checkDone(data); }); } 并行异步操作麻烦,并且无法保证返回值顺序
Promise - all // async - callbacks getData: function(city, callback)
{ var count = 0, datas = []; function checkDone(data) { datas.push(data); if (++count == 2) { callback(datas); } } this.getLayoutData(city, function(data) { checkDone(data); }); this.getOnsaleData(function(data) { checkDone(data); }); } // async - promise getData: function(city) { return Promise.all([ this.getLayoutData(city), this.getOnsaleData() ]); } 并行异步操作麻烦,并且无法保证返回值顺序
Promise - api • Promise.all(promises) - 并行处理异步,全部结束时返回值 • Promise.race(promises) -
并行处理异步,第一个结束时返回值 • Promise.resolve(value) - 生成一个成功的 Promise 对象 • Promise.reject(reason) - 生成一个失败的 Promise 对象 • promise.then(onFullfilled, onRejected) - 处理回调 • promise.catch(onRejected) - 处理失败回调 Static Prototype
• caniuse • RSVP.js • Q.js • https://promisesaplus.com/ • http://www.html5rocks.com/en/tutorials/es6/promises/
• https://www.youtube.com/watch?v=qbKWsbJ76-s • https://www.youtube.com/watch?v=wc72cyYt8-c can I use Promise?
感想
感想 • “迷途知返”- 要及时跳出坑
感想 • “迷途知返”- 要及时跳出坑 • 写注释可以帮助重新梳理逻辑与优化代码
感想 • “迷途知返”- 要及时跳出坑 • 写注释可以帮助重新梳理逻辑与优化代码 • 极致体验精益求精是我们永无止境的追求
感想 • “迷途知返”- 要及时跳出坑 • 写注释可以帮助重新梳理逻辑与优化代码 • 极致体验精益求精是我们永无止境的追求 • 把每一个项目都当做是一次超越自己的机会
感想 • “迷途知返”- 要及时跳出坑 • 写注释可以帮助重新梳理逻辑与优化代码 • 极致体验精益求精是我们永无止境的追求 • 把每一个项目都当做是一次超越自己的机会
• ES6 is Comming !!!
None
THE END.