代码与预期行为相反

时间:2014-07-10 18:52:43

标签: javascript html5 if-statement for-loop session-storage

民间,

我正在使用会话存储来创建最近访问的帐户列表。

我正在尝试从会话存储中删除状态为“DISABLED”的帐户。

队列可以包含6个点和任何我想要完全删除的禁用帐户。

但是,当我在我的机器上本地运行时,我看到的唯一帐户被禁用。 我究竟做错了什么?任何帮助都会很棒。

  "use strict";

/**
 * Manages the Recently Accessed for customers and orders
 * It uses HTML5 Session Storage
 * When we get the recently accessed items from Session Storage, we get it as string
 * Convert that into an array with JSON.parse
 * Check against the current item and insert all items into a queue
 * Sort and remove dupes
 * Now add the new item
 * Convert the queue to an array so we can manage it
 * Then convert it to a string  with JSON.stringify
 * Store it back into the Session Storage
 */
(function($, window, undefined) {

    TM.SALES.RecentlyAccessed = {

        /**
         * Store recently accessed customer or order into an HTML session storage
         * If session storage exists, add items from the session storage back into the queue
         * If there is a dupe, newest dupe wins
         * @param data is the account or order to be stored in recently accessed
         * @param key is the name of the primary key for example orderId or accountId
         * @param sessionStorageVariable is the name of the variable that is in the sessionStorage
         */
        memorize: function(data, key, sessionStorageVariable) {
            //TODO:  use try - catch to catch any errors when writing to session storage may fail

            if (typeof(Storage) !== "undefined") {
                var queue = new window.TM.SALES.Queue(6);

                // Get existing recently accessed items 
                var itemsAsString = sessionStorage.getItem(sessionStorageVariable);
                var items = JSON.parse(itemsAsString);
                var index = null;

                // Add items from the session storage back into the queue
                if (items !== null) {
                    for (var i = items.length; i > 0; i--) {
                        if (key === 'orderId') {
                            if (data.orderId !== items[i - 1].orderId) {
                                // Add to queue only if not a dupe
                                queue.enqueue(items[i - 1]);
                            }
                            } else if (key === 'accountId') {
                                // only add to the queue if the account is not
                                // a dupe and the account is not disabled
                                if (data.accountId === items[i - 1].accountId) {
                                    continue;
                                }
                                if(data.status==='DISABLED') {
                                    continue;
                                }

                                queue.enqueue(items[i - 1]);
                            }
                        }
                    }

                // Now add the new item
                queue.enqueue(data);

                // Convert the queue to an array
                // Session storage can store only strings, so convert the array to a string
                var sessionStorageString = JSON.stringify(queue.toArray());
                sessionStorage.setItem(sessionStorageVariable, sessionStorageString);
            }
        }
    };

})(jQuery, window);

0 个答案:

没有答案