Manu Chawla
Manu Chawla

Reputation: 327

Locally Persist data in browser

I am exploring elm to create the sample application, I want to store some data in the local storage of browser.

Currently i am working on 0.17.1 version. so any one please help me to solve this feature.

Upvotes: 0

Views: 153

Answers (2)

halfzebra
halfzebra

Reputation: 6807

As of today there are no modules published on the official package repository, do it through Ports for now.


If you feel adventurous and really want to get it to work asap, you can use fredcy/localstorage

Here's how you hack it

Clone fredcy/localstorage in the root of your project.

You will probably want to remove the VCS root.

Add elm-lang/dom as a dependency to your project.

$ git clone [email protected]:fredcy/localstorage.git
$ rm -rf localstorage/.git
$ elm package install elm-lang/dom -y

Add the following to your ./elm-package.json:

"source-directories": [
    ".",
    "localstorage/src",
    "localstorage/src/Native"
],
"native-modules": true,

And the worst part, go in to localstorage/src/Native/LocalStorage.js and replace:

3 // var _fredcy$localstorage$Native_LocalStorage = function()
4 var _user$project$Native_LocalStorage = function()

Advantages?

Having Elm-driven API for the Local Storage might be worth the hassle, but this stuff will break in the future.

Go with Ports.

Upvotes: 1

Tosh
Tosh

Reputation: 36030

TodoMVC implementation actually gives you a fair example how you can access localstorage via ports. I suggest you to take a look.

https://github.com/evancz/elm-todomvc

Upvotes: 3

Related Questions