ember-localstorage-adapter icon indicating copy to clipboard operation
ember-localstorage-adapter copied to clipboard

Relationships end up in an inconsistent state

Open lolmaus opened this issue 10 years ago • 3 comments

Hi!

I'm building a hierarchical todo list, using HollyApp as a good example.

My only model looks like this:

Task = DS.Model.extend
  title:    DS.attr 'string'
  position: DS.attr 'number'

  parent:   DS.belongsTo 'task', inverse: 'children'
  children: DS.hasMany   'task', inverse: 'parent'

As you can see, the model relates to itself. This lets nesting models into each one another.

The problem is that after i create a bunch of items and drag them into one another, data in local storage eventually ends up in an inconsistent state: parents do not correspond to children.

Here's my app's prototype: https://github.com/lolmaus/taskaparilla/commit/bc67f44ee757e619e96b7b377589c4e066daffa8 (You need to run Ruby's bundle install in addition to bower install and npm install).

Start the app, create a handful of tasks, then drag them into one another. The app is configured to .save() on every drag. After a while of drag-and-dropping, refresh the page. You'll see that apps end up in a different order (if the order didn't change, do some more dragging and refresh again).

Here's an example of a broken local storage content:

{
  "task":{
    "records":{
      "h1ed3":{
        "id":"h1ed3",
        "title":"One",
        "position":0,
        "parent":null,
        "children":[
          "5nskn"
        ]
      },
      "mv0kq":{
        "id":"mv0kq",
        "title":"Two",
        "position":0,
        "parent":"5nskn",
        "children":[ ]
      },
      "5nskn":{
        "id":"5nskn",
        "title":"Three",
        "position":0,
        "parent":"h1ed3",
        "children":[ ]
      }
    }
  }
}

As you can see, task Three (id 5nskn) has task Two (id mv0kq) as parent, but task Two does not have task Three as a child.

This local storage content results in the following Ember Data hierarchy:

- One
  - Three
- Two

Now, if i drag Two into Three, local storage content will not change. This means that after i refresh the page, the hierarchy will revert to the previous state.

I inspected the Ember Data content with Ember Inspector and discovered that after moving Two into Three, Ember Data content is consistent. All parents correspond to children correctly.

Please confirm whether this is a legit bug.

lolmaus avatar Apr 20 '15 23:04 lolmaus

I've fixed some bugs in my prototype and added some debug console output:

https://github.com/lolmaus/taskaparilla/commit/56f829f7c49c6fffdf6c5c9abca2cce7d542a712

Start from an empty list, create a number of tasks and start rearranging them while looking into browser console.

You'll see that the info (reported from iterating this.store.all()) is always correct.

But once you refresh the page, item positions are messed up as well as item parents/children.

PS I haven't implemented automatic position correction, so once ember-localstorage-adapter messes up positions, they will remain incorrect until you remove all items and start from scratch.

lolmaus avatar Apr 20 '15 23:04 lolmaus

@lolmaus unfortunately, I'm not currently in any project using LS :( did you find a fix for this?

kurko avatar Jun 12 '15 00:06 kurko

After over two years, this is still an issue!

CrushedPixel avatar Dec 03 '17 16:12 CrushedPixel