funkensturm / ember-local-storage

The addon provides a storageFor computed property that returns a proxy and persists the changes to localStorage or sessionStorage. It ships with an ember-data adapter.
https://www.funkensturm.com/ember-local-storage/
MIT License
218 stars 76 forks source link

Q: how to update nested array? #260

Closed songoo closed 5 years ago

songoo commented 7 years ago

Hello i tried object/array generated localStorage property.

In array for example i have objects that have nested array , like this :

[ {hiddenColumns;["name1","name2"],id:11}, {hiddenColumns;["name1","name7"],id:22} ]

In controller i get it as columns: storageFor('stored-columns'),

then i get it .get("columns") and filterBy record with id 22, then i set it new hiddenColumns array, storedColumnSetting.hiddenColumns = ["name4","name6"] .

Even thought at that moment i see in chrome debbuger that .get("columns") has changed values they are not stored in localstore and after refresh there are old ones ["name1","name7"] .

I tried similar scenario with Object and could not save it neither, maybe i am just missing some basics of usage please advice me .

fsmanuel commented 7 years ago

@songoo ember can not track nested objects. Where is the id comming from? If it is a ember-data model you can use the model option. It will scope the localstorage key to that model (type:id). Then you no longer need an array of objects but instead use just a plain array.

songoo commented 7 years ago

Hi, tnx for reply! Yes id are from ember-data models indeed, but that was just example, model option looks cool, but I have much more other arrays like "hiddenColumns" so i would have to do a lot of them .

"ember can not track nested objects" - i always tend to forget that, I changed implementation so whenewer there is change in hidden columns or other sub arrays i pop that oubject out and push it back, after that everything is refreshed fine.

Tnx again! :) 👍

fsmanuel commented 7 years ago

@songoo Sounds like a good solution. Do you mind to add a section with an example on the readme with your solution so others can use the approach as well?

fsmanuel commented 7 years ago

@songoo ping