{"action":"created","issue":{"url":"https://api.github.com/repos/jeromegn/Backbone.localStorage/issues/177","repository_url":"https://api.github.com/repos/jeromegn/Backbone.localStorage","labels_url":"https://api.github.com/repos/jeromegn/Backbone.localStorage/issues/177/labels{/name}","comments_url":"https://api.github.com/repos/jeromegn/Backbone.localStorage/issues/177/comments","events_url":"https://api.github.com/repos/jeromegn/Backbone.localStorage/issues/177/events","html_url":"https://github.com/jeromegn/Backbone.localStorage/issues/177","id":74749177,"number":177,"title":"Why records property is not updating with fetch?","user":{"login":"DmitryFillo","id":1942495,"avatar_url":"https://avatars0.githubusercontent.com/u/1942495?v=3","gravatar_id":"","url":"https://api.github.com/users/DmitryFillo","html_url":"https://github.com/DmitryFillo","followers_url":"https://api.github.com/users/DmitryFillo/followers","following_url":"https://api.github.com/users/DmitryFillo/following{/other_user}","gists_url":"https://api.github.com/users/DmitryFillo/gists{/gist_id}","starred_url":"https://api.github.com/users/DmitryFillo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DmitryFillo/subscriptions","organizations_url":"https://api.github.com/users/DmitryFillo/orgs","repos_url":"https://api.github.com/users/DmitryFillo/repos","events_url":"https://api.github.com/users/DmitryFillo/events{/privacy}","received_events_url":"https://api.github.com/users/DmitryFillo/received_events","type":"User","site_admin":false},"labels":[{"id":550039976,"url":"https://api.github.com/repos/jeromegn/Backbone.localStorage/labels/bug","name":"bug","color":"b60205","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2015-05-09T20:12:41Z","updated_at":"2017-03-25T11:39:59Z","closed_at":null,"body":"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.\n\n```\nbindStorageEvent : function() {\n    var _this = this;\n    $(window).on('storage.'+this.localStorage.name, function(e) {\n        if(e.originalEvent.key === _this.localStorage.name) {\n            _this.fetch();\n        }\n    });\n},\n\nunbindStorageEvent : function() {\n    $(window).off('storage.'+this.localStorage.name);\n}\n```\n\nBut this shouldn't work as expetcted because [records property](https://github.com/jeromegn/Backbone.localStorage/blob/master/backbone.localStorage.js#L73) will not be updated with fetch and [save() method use records prop](https://github.com/jeromegn/Backbone.localStorage/blob/master/backbone.localStorage.js#L80). And so all actual data which came from one tab will be rewrited by old data from another tab.\n\nSo I rewrited code in this way:\n\n```\nbindStorageEvent : function() {\n    var _this = this;\n    $(window).on('storage.'+this.localStorage.name, function(e) {\n        if(e.originalEvent.key === _this.localStorage.name) {\n\n            // the dirtiest records update way, only for test\n            var store = _this.localStorage.localStorage().getItem(_this.localStorage.name);\n            _this.localStorage.records = (store && store.split(\",\")) || [];\n\n            _this.fetch();\n        }\n    });\n},\n\nunbindStorageEvent : function() {\n    $(window).off('storage.'+this.localStorage.name);\n}\n```\n\nAnd it works great.\n\nSuggested 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.\n"},"comment":{"url":"https://api.github.com/repos/jeromegn/Backbone.localStorage/issues/comments/289206618","html_url":"https://github.com/jeromegn/Backbone.localStorage/issues/177#issuecomment-289206618","issue_url":"https://api.github.com/repos/jeromegn/Backbone.localStorage/issues/177","id":289206618,"user":{"login":"scott-w","id":1969937,"avatar_url":"https://avatars2.githubusercontent.com/u/1969937?v=3","gravatar_id":"","url":"https://api.github.com/users/scott-w","html_url":"https://github.com/scott-w","followers_url":"https://api.github.com/users/scott-w/followers","following_url":"https://api.github.com/users/scott-w/following{/other_user}","gists_url":"https://api.github.com/users/scott-w/gists{/gist_id}","starred_url":"https://api.github.com/users/scott-w/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scott-w/subscriptions","organizations_url":"https://api.github.com/users/scott-w/orgs","repos_url":"https://api.github.com/users/scott-w/repos","events_url":"https://api.github.com/users/scott-w/events{/privacy}","received_events_url":"https://api.github.com/users/scott-w/received_events","type":"User","site_admin":false},"created_at":"2017-03-25T11:39:59Z","updated_at":"2017-03-25T11:39:59Z","body":"I'm removing the `2.0` milestone until I can understand what needs to happen here. I'm thinking it _might_ be a bug but I'm not sure."}}
{"actor":{"display_login":"scott-w"}}