0

Hello in some function I've got code which save object {} to localForage which is nothing but asynchronous localStorage

if(vm.convertedColors[vm.convertedColors.length - 1].colorInRGB &&
   vm.convertedColors[vm.convertedColors.length - 1].colorInHSL &&
   vm.convertedColors[vm.convertedColors.length - 1].colorInHEX) {
        $localForage.setItem('colors', {
            inHSL: vm.convertedColors[vm.convertedColors.length - 1].colorInHSL,
            inHEX: vm.convertedColors[vm.convertedColors.length - 1].colorInHEX,
            inRGB: vm.convertedColors[vm.convertedColors.length - 1].colorInRGB
        }).then(function() {
            $rootScope.$emit('localForageUpdated');
        });
}

This run ones after button click and store object colors. It is working good but when I change color and click button again I want it to save new colorsobject instead of replacing it how it's working now.

So I would like to save colors1 colors2 colors3 and so on. It can be array of objects. s.o.s

2 Answers 2

2

Save it as an array. Then before saving the next one, get the old value, push the new object onto it, and save that.

$localForage.getItem('colors').then(function(colors) {
    colors = colors || []; // initialize as empty array on the first run
    colors.push({
        inHSL: vm.convertedColors[vm.convertedColors.length - 1].colorInHSL,
        inHEX: vm.convertedColors[vm.convertedColors.length - 1].colorInHEX,
        inRGB: vm.convertedColors[vm.convertedColors.length - 1].colorInRGB
    });
    $localForage.setItem('colors', colors).then(function() {
        $rootScope.$emit('localForageUpdated');
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

You'd want to have $localForage.getItem('colors') somewhere.
@BrahmaDev The first line.
Why do I need getItem at the first line? On first function run I will not have any data in cache, will this run then?
You need to get the item so you can append the new object to the array. The first time it will return null, and the line colors = colors || [] will substitute an empty array for that.
2

Just store colors as an array and push to that array whenever you want to push a new color. Then push the array back to local storage.

if (vm.convertedColors[vm.convertedColors.length - 1].colorInRGB &&
    vm.convertedColors[vm.convertedColors.length - 1].colorInHSL &&
    vm.convertedColors[vm.convertedColors.length - 1].colorInHEX) {

    $localForage
        .getItem("colors")
        .then((colors) => {
            colors = colors || [];
            colors.push({
                inHSL: vm.convertedColors[vm.convertedColors.length - 1].colorInHSL,
                inHEX: vm.convertedColors[vm.convertedColors.length - 1].colorInHEX,
                inRGB: vm.convertedColors[vm.convertedColors.length - 1].colorInRGB
            });
            return $localForage.setItem("colors", colors);
        })
        .then((err) => {
            if (err) {
                console.error(err);
            }
            return $localForage.getItem("colors")
        })
        .then((colors) => {
            console.log(colors);
        });
}

2 Comments

@Andreas I'm sorry, I was probably working on my answer at the same time. Do you want me to delete my answer?
There are enough differences, like returning the promise instead of calling it directly, you wrapped it in the if.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.