Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findAll() returns array and breaks model.fetch() #231

Open
heeplr opened this issue Nov 5, 2022 · 0 comments
Open

findAll() returns array and breaks model.fetch() #231

heeplr opened this issue Nov 5, 2022 · 0 comments

Comments

@heeplr
Copy link

heeplr commented Nov 5, 2022

findAll() at https://github.com/jeromegn/Backbone.localStorage/blob/master/src/sync.js#L34 returns an array which is not taken care for.

When using a model with defaults set, fetch() puts the array into the model attributes. This has multiple implications.

this:

export const Config = Model.extend({
    localStorage: new LocalStorage('Config'),
    defaults: {
        foo: 'bar',
    }
})

const config = new Config()
config.set("foo":"baz")
config.save()
config.fetch()

results in this

>> model.attributes
{
  "0": {
    "foo": "baz",
    "id": "b65b72ba-980f-30f5-dfc3-ae4bfb6dfb94"
  },
  "foo": "bar",
}

Since there is never an id, more arrays get added during the life of the model.

I think a possible fix could be to choose the first element returned by findAll():

try {
    switch (method) {
      case 'read':
-        resp = (0, _underscore.isUndefined)(model.id) ? store.findAll() : store.find(model);
+        resp = (0, _underscore.isUndefined)(model.id) ? store.findAll()[0] : store.find(model);
        break;
      case 'create':
   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant