Created
November 25, 2012 23:34
-
-
Save cesarvarela/4145876 to your computer and use it in GitHub Desktop.
A VERY basic chrome.storage.local custom sync, method for Backbone.js Models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createLocalStorageSync = function(storageKey) | |
{ | |
return function(method, model, options) | |
{ | |
switch (method) | |
{ | |
case 'create': | |
case 'update': | |
chrome.storage.local.set({storageKey: JSON.stringify(model) }, options.success ); | |
break; | |
case 'delete': | |
chrome.storage.local.remove(storageKey, options.success ); | |
break; | |
case 'read': | |
chrome.storage.local.get(storageKey, function(items) | |
{ | |
options.success( items[storageKey] && JSON.parse(items[storageKey]) || {} ); | |
}); | |
break; | |
} | |
return this; | |
}; | |
}; | |
// usage | |
SettingsModel = Backbone.Model.extend | |
({ | |
// override sync with the custom sync method, and declare a key to be used when saving to localStorage | |
sync: createLocalStorageSync('settings'), | |
defaults: | |
{ | |
'notifications-onmessage': true | |
} | |
}); | |
This DOES NOT work on collections. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment