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
| Ember.Application.initializer({ | |
| name: "storageService", | |
| initialize: function(container, application) { | |
| application.register('service:storage', application.StorageService, { | |
| singleton: true | |
| }); | |
| application.inject('route', 'storage', 'service:storage'); |
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
| init: function() { | |
| var callback = this._handleStorageEvent.bind(this); | |
| $(window).on('storage', callback); | |
| } | |
| _handleStorageEvent: function(event) { | |
| var storageEvent = event.originalEvent; | |
| var storageKey = storageEvent.key; | |
| var tokens = storageKey.split(':'); |
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
| setUnknownProperty: function(key, value) { | |
| var namespacedKey = this._key(key); | |
| var payload = this._serialize(value); | |
| this.get('persistence').setItem(namespacedKey, payload); | |
| this.notifyPropertyChange(key); | |
| return true; | |
| }, | |
| _serialize: function(value) { | |
| return JSON.stringify(value); |
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
| unknownProperty: function(key) { | |
| var namespacedKey = this._key(key); | |
| var payload = this.get('persistence').getItem(namespacedKey); | |
| return this._deserialize(payload); | |
| }, | |
| _deserialize: function(value) { | |
| return JSON.parse(value); | |
| }, |
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
| App.StorageService = Ember.Object.extend({ | |
| persistence: window.localStorage, | |
| namespace: 'ember-storage-service', | |
| // ... | |
| }); |
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
| function saveItem(key, value) { | |
| var json = JSON.stringify(value); | |
| localStorage.setItem(key, json); | |
| } | |
| function loadItem(key) { | |
| var json = localStorage.getItem(key); | |
| return JSON.parse(json); | |
| } |
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
| localStorage.setItem('numbers', [1, 2, 3]); | |
| localStorage.getItem('numbers') // => "1,2,3" | |
| var hash = { hello: 'world' } | |
| localStorage.setItem('myHash', hash); | |
| localStorage.getItem('myHash') // => "[object Object]" |
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
| if (!window.localStorage) { | |
| alert("Your browser doesn't support local storage!"); | |
| } | |
| // method based approach | |
| localStorage.setItem('textSize', 'giant'); | |
| localStorage.getItem('textSize'); // => "giant" | |
| // index accessor approach | |
| localStorage['selectedTab'] = 'preferences' |
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
| App.StorageService = Ember.Object.extend({ | |
| persistence: window.localStorage, | |
| namespace: 'ember-storage-service', | |
| init: function() { | |
| var callback = this._handleStorageEvent.bind(this); | |
| $(window).on('storage', callback); | |
| }, | |