Riot.js tips - preventUpdate ã使ã£ã¦ã¹ã¯ãã¼ã«æã®å¤§é update() ãæ¢ããã
ãyieldã ã«å¼ãç¶ãããã¢ãã¯ãª Riot.js tips ã§ã.
å æ¥ä¸è¨ã®ãããªãã¤ã¼ããæµãã¦ãã¾ãã.
ã¡ãã£ã¨ããã¯é©æã®äºå®ãã...riotjsã§ã¿ã°ã§ç´æ¥onscroll="{hoge}"ã£ã¦ããã¡ã§bindãããããï¼ã¹ã¯ãã¼ã«ãã度ã«ãupdatedãå¼ã°ãããããã¤ã¾ããã¹ã¯ãã¼ã«ãã度ã«update()ãããã£ã¦ããããï¼ï¼
— æz (@AnzNetJp) 2016å¹´10æ27æ¥
ãããªãç§ãæããã«ããã£ããã¨ããã£ãã®ã§, ãã®å¯¾å¦æ³ãç´¹ä»ãããã¨æãã¾ã.
ãã¢ãµã³ãã«
ã¾ãã¯ãã¢ã§ã.
ã¹ã¯ãã¼ã«ããã¨ã³ã³ã½ã¼ã«ã« updated
ã¨è¡¨ç¤ºããã¾ã.
大éã« update()
ãããªããã対å¿ãã¦ããã®ã§ããã»ã©ãããã表示ãããªãã®ãããããã¨æãã¾ã.
Riot.js ã«ãããã¤ãã³ãçºç«æã®ã©ã¤ããµã¤ã¯ã«
Riot.js ã¯, onã»ã«ããã
ã«è¨å®ãã¦ããé¢æ°ãå¼ã¶ã¨èªå㧠update
ãå¼ã¶ä»æ§ã«ãªã£ã¦ãã¾ã.
onclick
ã ontouchstart
ã¨ãã£ãããã»ã©é »ç¹ã«çºç«ããªãã¤ãã³ããªãããã§ãè¯ãã®ã§ãã,
onscroll
ã oninput
ã¨ãã£ãçæéã«å¤§éã«çºç«ãããããªã¤ãã³ãã®å ´å, ããªãã®è² è·ã«ãªã£ã¦ãã¾ãã¾ã.
Riot.js ã®ç¬èªã¤ãã³ãã«å«ã¾ãã preventUpdate ã使ãã
Riot.js ã§ç»é²ããé¢æ°ãå®è¡ãããéã«æ¸¡ãããã¤ãã³ããªãã¸ã§ã¯ãã«ã¯
preventUpdate
ã¨ãããã©ã°ãç¨æããã¦ãã¾ã.
å®ã¯ãã®å¤ã false
ã®ã¨ãã®ã¿è£å´ã§ update()
ãèµ°ãããã«ãªã£ã¦ãã¾ã.
Riot.js å ã®å®è£ ã³ã¼ãã¯ãã¡ã
if (!e.preventUpdate) {
el = item ? getImmediateCustomParentTag(ptag) : tag
el.update()
}
é¢æ°å®è¡å¾ã«ä¸ã®å¦çãå¼ã°ã preventUpdate
ã®å¤ã«ãã£ã¦
update()
ãããã©ããåå²ãã¦ããã®ãããããã¨æãã¾ã.
ããã©ã«ãå¤ã¯ false
ãªã®ã§ãã®å¤ã true
ã«ããã° update()
ãå¼ã°ããªããªãã¾ã.
<div class='box' onscroll='{scroll}'></div>
this.scroll = function(e) {
e.preventUpdate = false;
}
ä¸å®ééã§æ示çã« update()
ãå¼ã¼ã
ä¸è¨ã®å¯¾å¿ãå
¥ããã ãã 㨠update()
ãå
¨ãå¼ãã§ãããªããªãã®ã§ããã¯ããã§å°ãã¾ããã.
Riot.js ã§ã¯æ示çã« update()
ã¡ã½ãããå®è¡ãããã¨ã§ã©ã¤ããµã¤ã¯ã«ãã³ã³ããã¼ã«ãããã¨ãã§ãã¾ã.
setTimeout()
ã¨çµã¿åããããã¨ã§ãupdate()
ã®é »åº¦ãåçã«ä¸ãããã¨ãã§ãã¾ã.
ã³ã¼ãã¯ä¸è¨ã®ããã«ãªãã¾ã.
this._scrollTimeoutId = null;
this.scroll = function(e) {
// èªå update ã off ã
e.preventUpdate = true;
if (this._scrollTimeoutId) {
window.clearTimeout(this._scrollTimeoutId);
}
this._scrollTimeoutId = window.setTimeout(function() {
// ããã§æ示çã« update ãå¼ã¶
self.update();
self._scrollTimeoutId = null;
}, 32);
};
ã¡ãã£ã¨è¤éã«è¦ãã¾ãããã£ã¦ãããã¨ã¯åç´ã§ã.
setTimeout()
ã® id ãä¿æãã¦ããã¦, ä¸å®éé(ä»åã¯32ããªç§
)以å
ã«ããä¸åº¦ã¤ãã³ããçºç«ããå ´å
åã®å¦çããã£ã³ã»ã«ãã¦å度 setTimeout()
ã§å¦çãç»é²ããªããã¦ãã¾ã.
ãããããã¨ã§ä¸å®éé以å
ã«é£ç¶ã§çºçããã¤ãã³ãããã¹ã¦ãã£ã³ã»ã«ã, æå¾ã®ã¤ãã³ãæã®ã¿ update()
ãå®è¡ãããã¨ãã§ãã¾ã.
ãã¢ã®ã³ã¼ã
ãã¢ã® Riot.js é¨åã®ã³ã¼ãã§ã.
<app>
<h1>Riot.js tips - preventUpdate ã使ã£ã¦ã¹ã¯ãã¼ã«æã®å¤§é update ãæ¢ããã</h1>
<div class='box', onscroll='{scroll}'>
<div class='pad'>pad</div>
</div>
<style>
.box {
width: 200px;
height: 200px;
background-color: white;
box-shadow: 0px 0px 4px 0px #aaa;
overflow: scroll;
}
.pad {
height: 1200px;
padding: 4px;
}
</style>
var self = this;
this.title = opts.title;
this._scrollTimeoutId = null;
this.scroll = function(e) {
// èªå update ã off ã
e.preventUpdate = true;
if (this._scrollTimeoutId) {
window.clearTimeout(this._scrollTimeoutId);
}
this._scrollTimeoutId = window.setTimeout(function() {
// ããã§æ示çã« update ãå¼ã¶
self.update();
self._scrollTimeoutId = null;
}, 32);
};
this.on('updated', function() {
console.log('update ãã¾ãã!');
});
</app>
ãã®å¦çãå¿ç¨ãããã¨ã§ input search ã§å ¥åãè½ã¡çããã¿ã¤ãã³ã°ã§æ¤ç´¢ãããã, ãªãã¦ãã¨ãã§ãã¾ã.
ããã次åãµã³ãã«ã交ãã¦ç´¹ä»ãããã¨æãã¾ã.