localStorage
Cart、Viewed、Trash などのいくつかのキーがあります。
問題は 2 つあります。
1) localStorage 内の項目 ID を最もパフォーマンスの高い方法でループし、ID が既に存在する場合は、一致する DOM 要素にクラスまたはデータ属性を追加する方法を教えてください。
2) 3 つの localStorage Keys
(ゴミ箱、カート、閲覧済み) から最後の 20 個のアイテムの個別のリストを作成する最良の方法は何ですか? パフォーマンスの観点から、私の質問の pt1 と同じ Loop 関数でこれを行うのが最善でしょうか (つまり、ID が存在するかどうかをテストし、true の場合はクラスを追加します)?
これは私が持っているものです:
HTML
<article data-id="548" data-date="Mon Dec 19 09:12:57">
<h1><a href="#">Title</a></h1> // section // footer <a href="#" data-context="trash">trash</a>
</article>
jQuery
$tools.on("click", "a", function(e){
var storeId = $(this).attr('data-context');
var selector = $(this).closest('article');
var dataId = selector.attr('data-id');
var pubDate = selector.attr('data-date');
var postUrl = selector.find('a', 'h1').attr('href');
var postTitle = selector.find('h1').text();
var $removable = $container.find( selector );
setLocalStorage(storeId, dataId, postUrl, postTitle, pubDate);
if (storeId == "Trash") {
$container.isotope( 'remove', $removable );
};
e.preventDefault();
});
セッター関数。
変数はdata-context="Trash"
(キー) やdata-id="001"
(id) などを介して渡され、関連するキーに格納されます。
function setLocalStorage(itemKey, dataId, postUrl, postTitle, postPub) {
var ItemKey = itemKey;
var timeStamp = new Date();
var json = JSON.parse(localStorage.getItem(ItemKey));
if(json == null)
json = [];
json.push({id:dataId,title:postTitle,url:postUrl,postPub:postPub,dataTimeStamp:timeStamp});
// save the result array
sessionStorage.setItem(ItemKey, JSON.stringify(json))
// Notify user item added to Cart
updateNotifications(ItemKey, json);
}
localStorage は次のようになります。Trash [{"id":"418","title":"\n\t\t\t\t\t\t\t\t\tPost Title....//..."}]
ここで何か助けていただければ幸いです。
あけましておめでとう!
乾杯ベン