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

a trouble about the fetch function #142

Closed
qkhhimkfrgw opened this issue Jul 29, 2014 · 3 comments
Closed

a trouble about the fetch function #142

qkhhimkfrgw opened this issue Jul 29, 2014 · 3 comments

Comments

@qkhhimkfrgw
Copy link

Hi! Thank you for reading my problem about Backbone.localStorage.

I'm sorry to tell that may be I had found a bug.

I'm not sure if it is a bug or not, but I have a problem like this:

// create one record without specifing an id and the id is generated by the localStorage.guid() function
var collection = new ModelCollection();
collection.createTestData();
collection.fetch({async: false});
collection.at(0).destroy();
// create one record again
collection.createTestData();
collection.fetch({async: false});
if (collection.size() > 0) {
    collection.at(0).destroy();
} else {
    alert('data not found');
}

The step is that:

  1. createTestData() // create one random record
  2. fetch() // the record is getted
  3. destroy() // the key point!
  4. createTestData() // create one random record agian
  5. fetch() // fetch again, but data not found

As you see, the record won't be found in the second time if I delete records before and do not specify an id and the id is generated by the localStorage.guid() function.

How can I solve the problem? Do I have to specify one id?

@jeromegn
Copy link
Owner

jeromegn commented Aug 4, 2014

What's that {async: true} in the collection fetch method?

Can you add tests to this project to show me how this is failing?

@qkhhimkfrgw
Copy link
Author

Here is the full source of one test.

<body>
    <script src="js/vendor/jquery-1.11.0.min.js"></script>
    <script src="js/vendor/underscore.js"></script>
    <script src="js/vendor/backbone.js"></script>
    <script src="js/vendor/backbone.localStorage.js"></script>
    <script>
        var TestModel = Backbone.Model.extend({
            defaults: {
                field: ''
            },
            localStorage: new Backbone.LocalStorage('tests-backbone')
        });
        var TestCollection = Backbone.Collection.extend({
            model: TestModel,
            localStorage: new Backbone.LocalStorage('tests-backbone')
        });
        window.localStorage.clear();
        var collection = new TestCollection();
        collection.create({field: '1'});
        collection.fetch();
        if (collection.size() > 0) {
            collection.invoke('destroy');
            alert('data found');
        }
        else {
            alert('data not found');
        }
    </script>    
</body>

It shows me 'data not found' in the real chrome browser,
but 'data found' in the jsfiddle(http://jsfiddle.net/qkhhimkfrgw/c32uL/5/).
If I change the TestModel definition like this:

var TestModel = Backbone.Model.extend({
            defaults: {
                field: ''
            },
           var TestModel = Backbone.Model.extend({
            defaults: {
                field: ''
            }//,
           // localStorage: new Backbone.LocalStorage('tests-backbone')
        });
});

It runs without any problems.
Maybe the localStorage of TestModel is the problem,
but I can not comment out the localStorage of TestModel.
Sometimes I use new TestModel ({...}).save(), and sometimes I will use TestCollection.create(...).

@scott-w
Copy link
Collaborator

scott-w commented Feb 28, 2017

I'm inclined to think that this is correct behaviour - model.destroy() is supposed to synchronise with the server/localstorage so I'd expect fetch to return nothing.

@scott-w scott-w closed this as completed Feb 28, 2017
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

3 participants