Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   localstorage service script (https://javascript.ru/forum/misc/67063-localstorage-service-script.html)

ExXxTaSy 26.01.2017 11:58

и не пойму ту библиотеку что вы бросили. где она хранит данные в обьекте? там проде бы 2 хранилища. sessionStorage, localStorage
мне это не подходит. в сафари в примат моде оба хранилища не работают. нужно именно в обьекте сервиса все хранить

ExXxTaSy 26.01.2017 14:01

var localStorage = angular.module('localStorage', []);

localStorage.factory('localStorageService', function() {
    var storageSupportedService;

    function isLocalStorageSupported() {
        var testKey = 'key', storage = window.localStorage;
        try {
            storage.setItem(testKey, '1');
            storage.removeItem(testKey);
            return localStorageName in window && window[localStorageName];
        } catch (e) {
            // return false;
            if (e == QUOTA_EXCEEDED_ERR) {
                //do obj storage

            }
        }
    }

    return storageSupportedService;
});

this.storageData = function () {

    module.exports = {
        name: 'memoryStorage',
        read: read,
        write: write,
        each: each,
        remove: remove,
        clearAll: clearAll
    };

    var memoryStorage = {};

    function read(key) {
        return memoryStorage[key]
    }

    function write(key, data) {
        memoryStorage[key] = data
    }

    function each(callback) {
        for (var key in memoryStorage) {
            if (memoryStorage.hasOwnProperty(key)) {
                callback(memoryStorage[key], key)
            }
        }
    }

    function remove(key) {
        delete memoryStorage[key]
    }

    function clearAll(key) {
        memoryStorage = {}
    }

};


что то такое пока что получается. гляньте кто то плз

ExXxTaSy 27.01.2017 12:49

вопрос решил так:
var fakeLocalStorage = function() {
        var fakeLocalStorage = {},
            storage;
         
        // If Storage exists we modify it to write to our fakeLocalStorage object instead.
        // If Storage does not exist we create an empty object.
        if (window.Storage && window.localStorage) {
            storage = window.Storage.prototype;
        } else {
            // We don't bother implementing a fake Storage object
            window.localStorage = {};
            storage = window.localStorage;
        }

        // For older IE
        if (!window.location.origin) {
            window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
        }

        var dispatchStorageEvent = function(key, newValue) {
            var oldValue = (key == null) ? null : storage.getItem(key), // `==` to match both null and undefined
                url = location.href.substr(location.origin.length),
                storageEvent = document.createEvent('StorageEvent'); // For IE,  

            storageEvent.initStorageEvent('storage', false, false, key, oldValue, newValue, url, null);
            window.dispatchEvent(storageEvent);
        };

        storage.key = function(i) {
            var key = Object.keys(fakeLocalStorage)[i];
            return typeof key === 'string' ? key : null;
        };

        storage.getItem = function(key) {
            return typeof fakeLocalStorage[key] === 'string' ? fakeLocalStorage[key] : null;
        };

        storage.setItem = function(key, value) {
            dispatchStorageEvent(key, value);
            fakeLocalStorage[key] = String(value);
        };

        storage.removeItem = function(key) {
            dispatchStorageEvent(key, null);
            delete fakeLocalStorage[key];
        };

        storage.clear = function() {
            dispatchStorageEvent(null, null);
            fakeLocalStorage = {};
        };
    };

    //
    if (typeof window.localStorage === 'object') {
        try {
            localStorage.setItem('localStorageTest', 1);
            localStorage.removeItem('localStorageTest');
        } catch (e) {
            fakeLocalStorage();
        }
    } else {
        // Use fake localStorage for any browser that does not support it.
        fakeLocalStorage();
    }


Часовой пояс GMT +3, время: 14:40.