question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why records property is not updating with fetch?

See original GitHub issue

I use localStorage collection, works great. But I want use storage event for updating collection in many tabs. Idea: do fetch() when storage event fires.

bindStorageEvent : function() {
    var _this = this;
    $(window).on('storage.'+this.localStorage.name, function(e) {
        if(e.originalEvent.key === _this.localStorage.name) {
            _this.fetch();
        }
    });
},

unbindStorageEvent : function() {
    $(window).off('storage.'+this.localStorage.name);
}

But this shouldn’t work as expetcted because records property will not be updated with fetch and save() method use records prop. And so all actual data which came from one tab will be rewrited by old data from another tab.

So I rewrited code in this way:

bindStorageEvent : function() {
    var _this = this;
    $(window).on('storage.'+this.localStorage.name, function(e) {
        if(e.originalEvent.key === _this.localStorage.name) {

            // the dirtiest records update way, only for test
            var store = _this.localStorage.localStorage().getItem(_this.localStorage.name);
            _this.localStorage.records = (store && store.split(",")) || [];

            _this.fetch();
        }
    });
},

unbindStorageEvent : function() {
    $(window).off('storage.'+this.localStorage.name);
}

And it works great.

Suggested solution: I think that we can put this code to the another method in the localStorage object (records_update) and call this method from constructor and from Backbone.sync() when doing fetch() or from findAll() before getting particular model. I think this property must be updated on fetch, not only at the collection initialization time.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
yahuarkunturcommented, Jun 12, 2018

Any updates on this issue? or is there any workaround?

Cheers!

0reactions
blikblumcommented, Dec 29, 2018

I fixed this in my backbone fork.

Look at the following commits: https://github.com/blikblum/backbone/commit/8054cdd740eb7d62489185ffb055ab9f97e0a7a1 https://github.com/blikblum/backbone/commit/e183cce5dc262ca91559c94230134db03ad40de2

Should not be hard to make a PR. Currently i’m on a slow internet connection so cannot do it myself

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using fetch to update the database and DOM without ...
Fantastic! The fetch and its config object are updating our information in the database, and or use of the Response JSON is updating...
Read more >
Laravel -, Can't update more than one record using Fetch Api
I want to update my multiple records. But only first record is updating, other ones aren't.. ... Then I'm trying to save them...
Read more >
How to Use the Fetch API (Correctly) - CODE Magazine
Navigate into the folder Samples-WebAPI and load that folder in Visual Studio Code or Visual Studio 2019. Open the appsettings.json file and ...
Read more >
Solved: Code not updating records - ServiceNow Community
Hello,. I have a background script that does not seem to be working. It is supposed to copy all field values from 1...
Read more >
Updating data when fetching
My second alternative is to simply do a separate call to update the data whenever it is fetched. Backend fetch -> DB select...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found