{"id":13747016,"url":"https://github.com/nilbus/Backbone.dualStorage","last_synced_at":"2025-05-09T08:31:05.045Z","repository":{"id":58245017,"uuid":"3274010","full_name":"nilbus/Backbone.dualStorage","owner":"nilbus","description":"A dual (localStorage and REST) sync adapter for Backbone.js","archived":true,"fork":false,"pushed_at":"2017-10-25T15:51:57.000Z","size":942,"stargazers_count":798,"open_issues_count":27,"forks_count":109,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-04-22T01:50:28.624Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nilbus.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-01-26T13:35:52.000Z","updated_at":"2025-02-27T07:37:42.000Z","dependencies_parsed_at":"2022-08-30T22:51:58.615Z","dependency_job_id":null,"html_url":"https://github.com/nilbus/Backbone.dualStorage","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilbus%2FBackbone.dualStorage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilbus%2FBackbone.dualStorage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilbus%2FBackbone.dualStorage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilbus%2FBackbone.dualStorage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilbus","download_url":"https://codeload.github.com/nilbus/Backbone.dualStorage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253216918,"owners_count":21872978,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T06:01:10.951Z","updated_at":"2025-05-09T08:31:04.781Z","avatar_url":"https://github.com/nilbus.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Libraries"],"sub_categories":[],"readme":"Backbone dualStorage Adapter v1.4.2\n===================================\n\nA dualStorage adapter for Backbone. It's a drop-in replacement for Backbone.Sync() to handle saving to a localStorage database as a cache for the remote models.\n\nUsage\n-----\n\nInclude Backbone.dualStorage after having included Backbone.js:\n\n    \u003cscript type=\"text/javascript\" src=\"backbone.js\"\u003e\u003c/script\u003e\n    \u003cscript type=\"text/javascript\" src=\"backbone.dualstorage.js\"\u003e\u003c/script\u003e\n\nCreate your models and collections in the usual way.\nFeel free to use Backbone as you usually would; this is a drop-in replacement.\n\nKeep in mind that Backbone.dualStorage really loves your models. By default it will cache everything that passes through Backbone.sync. You can override this behaviour with the booleans ```remote``` or ```local``` on models and collections:\n\n    SomeCollection = Backbone.Collection.extend({\n        remote: true // never cached, dualStorage is bypassed entirely\n        local: true  // always fetched and saved only locally, never saves on remote\n        local: function() { return trueOrFalse; } // local and remote can also be dynamic\n    });\n\nYou can also deactivate dualsync to some requests, when you want to sync with the server only later.\n\n    SomeCollection.create({name: \"someone\"}, {remote: false});\n\nData synchronization\n--------------------\n\nWhen the client goes offline, dualStorage allows you to keep changing and destroying records. All changes will be sent when the client goes online again.\n\n    // server online. Go!\n    people.fetch();       // load people models and save them into localstorage\n\n    // server offline!\n    people.create({name: \"Turing\"});   // you still can create new people...\n    people.models[0].save({age: 41});  // update existing ones...\n    people.models[1].destroy();        // and destroy as well\n\n    // collections track what is dirty and destroyed\n    people.dirtyModels()               // =\u003e Array of dirty models\n    people.destroyedModelIds()         // =\u003e Array of destroyed model ids\n\n    // server online again!\n    people.syncDirtyAndDestroyed();    // all changes are sent to the server and localStorage is updated\n\nKeep in mind that if you try to fetch() a collection that has dirty data, only data currently in the localStorage will be loaded. collection.syncDirtyAndDestroyed() needs to be executed before trying to download new data from the server.\n\nIt is possible to tell whether the operation succeeded remotely or locally by examining `options.dirty` in the `success` callback:\n\n\tmodel.save({\n\t\tname: \"Turing\"\n\t}, {\n\t\tsuccess: function(model, response, options) {\n\t\t\tif (options.dirty) {\n\t\t\t\t// saved locally\n\t\t\t} else {\n\t\t\t\t// saved remotely\n\t\t\t}\n\t\t}\n\t});\n\nOffline state detection\n-----------------------\ndualStorage **always** treats an Ajax status code of `0` as an indication it is working offline. Additional status codes can be added by setting `offlineStatusCodes` to either an array:\n\n    Backbone.DualStorage.offlineStatusCodes = [408];\n\nor a function that accepts the `response` object and returns an array:\n\n    Backbone.DualStorage.offlineStatusCodes = function(xhr) {\n        var codes = [];\n\n        if (...) {\n            codes.push(xhr.status);\n        }\n\n        return codes;\n    }\n\n\nData parsing\n------------\n\nSometimes you may want to customize how data from the remote server is parsed before it's saved to localStorage.\nTypically your model's `parse` method takes care of this.\nSince dualStorage provides two layers of backend, we need a second parse method.\nFor example, if your remote API returns data in a way that the default `parse` method interprets the result as a single record,\nuse `parseBeforeLocalSave` to break up the data into an array of records like you would with [parse](http://backbonejs.org/#Model-parse).\n\n* The model's `parse` method still parses data read from localStorage.\n* The model's `parseBeforeLocalSave` method parses data read from the remote _before_ it is saved to localStorage on read.\n\nLocal data storage\n------------------\n\ndualStorage stores the local cache in localStorage.\nEach collection's (or model's) `url` property is used as the storage namespace to separate different collections of data.\nThis can be overridden by defining a `storeName` property on your model or collection.\nDefining storeName can be useful when your url is dynamic or when your models do not have the collection set but should be treated as part of that collection in the local cache.\n\nInstall\n-------\n\nClone like usual or via npm `npm install nilbus/backbone.dualstorage --save`.\n\nContributing\n------------\n\nSee CONTRIBUTING.md\n\nAuthors\n-------\n\nThanks to [Edward Anderson](https://github.com/nilbus) for the test suite and continued maintenance.\nThanks to [Lucian Mihaila](https://github.com/lucian1900) for creating Backbone.dualStorage.\nThanks to [Jerome Gravel-Niquet](https://github.com/jeromegn) for Backbone.localStorage.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilbus%2FBackbone.dualStorage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilbus%2FBackbone.dualStorage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilbus%2FBackbone.dualStorage/lists"}