0

I wrote a bookmarklet that retrieves information from a page and stores it in JSON format in local storage (converting it to a string first, of course).

I would like a web app I am writing to be able to process this data, on the fly, preferably as it gets saved to the localStorage.

Right now i can change the item in LS via the console and refresh the page and the new data appears but I would like it to be live and seamless.

Any advice on how to go about this? I found several localStorage modules for angularJS and I tried them but they don't seem to allow me to retrieve from LS if the data is already there in LS.

In response to answer:

$scope.$watch(
    function(){
        return $window.localStorage.getItem('TestData');
    },
    function(newValueInStorage){
        $scope.testingLS = newValueInStorage;
    }
)

I tried this and I still get the data displayed by just doing a {{ testingLS }} in the view template but when I go and change the TestData key in local storage via the console it doesn't update instantly. (for now, I am just testing it without the bookmarklet with just a simple string inside TestData

1 Answer 1

1

There is few ways to do it

One of will be to populate correct model on scope when saving to localStorage

The other that I can think of at this moment is to setup watcher

$watch(
  function(){
    return localstorage object
  },
  function(newValueInStorage){
    $scope.modelFromLS = JSON.parse(newValueInsStorage)
  }
)

---edit--- as per James comment you need something that will handle the fact that data has changed in different tab and $digest process need to run for watch to be recalculated

http://plnkr.co/edit/zlS3wL65meBeA8KkV5KH?p=preview

window.addEventListener('focus', function(){
  console.log('focus')
  $scope.$digest()
})
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your response. The data in local storage will already be there or be modified by a completely different website on a different browser tab via a bookmarklet. I want to run the bookmarklet on that browser tab then switch back to my local website and the page be able to detect the change in LS and reflect that on the page. I will try this $watch code. Thanks again.
A watch alone won't work as you need something to 'happen' in the angular world for angular to trigger a digest and recalculate the watches. AFAIK switching tab isn't one of these events. There is a visibility API which will let you detect tab switches and manually trigger a digest but support is still patchy (IE10+): developer.mozilla.org/en-US/docs/Web/Guide/User_experience/… otherwise you can try hooking into window.onblur/onfocus which is also imperfect or use polling.
You are right James Gaunt beside one thing, pooling is not the only solution, i've extended my answer with a plunker
This works very well for my purposes. Thank you very much. This is an internal app for my own purposes so I am not overly worried about performance on a wide scale or anything. I really appreciate it a lot! :D
I'm glad to be helpful :)

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.