码迷,mamicode.com
首页 > 数据库 > 详细

[PWA] 16. Clean IDB

时间:2016-05-21 23:20:17      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:

When we save items to IndexDB, we need to think about clean the items or keep those in a short list.

 

IndexController.prototype._onSocketMessage = function(data) {
  var messages = JSON.parse(data);

  this._dbPromise.then(function(db) {
    if (!db) return;

    var tx = db.transaction(wittrs, readwrite);
    var store = tx.objectStore(wittrs);
    messages.forEach(function(message) {
      store.put(message);
    });

    // TODO: keep the newest 30 entries in ‘wittrs‘,
    // but delete the rest.
    //
    // Hint: you can use .openCursor(null, ‘prev‘) to
    // open a cursor that goes through an index/store
    // backwards.
    store.index(by-date).openCursor(null, prev)
        .then((cursor)=>{
          return cursor.advance(30); // only 30 itmes are kept
        }).then(function deleteCursor(cursor){
            if(!cursor){
              return;
            }else{
              cursor.delete();
              return cursor.continue().then(deleteCursor);
            }
        })
  });

  this._postsView.addPosts(messages);
};

 

[PWA] 16. Clean IDB

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5515785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!