├── vendor └── .gitkeep ├── tests ├── dummy │ ├── app │ │ ├── helpers │ │ │ └── .gitkeep │ │ ├── models │ │ │ └── .gitkeep │ │ ├── routes │ │ │ └── .gitkeep │ │ ├── components │ │ │ ├── .gitkeep │ │ │ └── storage-demo.js │ │ ├── controllers │ │ │ └── .gitkeep │ │ ├── templates │ │ │ ├── components │ │ │ │ ├── .gitkeep │ │ │ │ └── storage-demo.hbs │ │ │ ├── application.hbs │ │ │ └── index.hbs │ │ ├── resolver.js │ │ ├── router.js │ │ ├── app.js │ │ ├── styles │ │ │ └── app.css │ │ └── index.html │ ├── public │ │ ├── robots.txt │ │ └── crossdomain.xml │ └── config │ │ ├── targets.js │ │ └── environment.js ├── test-helper.js ├── helpers │ └── resolver.js ├── index.html └── unit │ └── services │ └── storage-test.js ├── .watchmanconfig ├── .bowerrc ├── index.js ├── app ├── services │ └── storage.js └── initializers │ └── storage-service.js ├── config ├── environment.js └── ember-try.js ├── .ember-cli ├── .npmignore ├── .gitignore ├── ember-cli-build.js ├── testem.js ├── .jshintrc ├── .editorconfig ├── LICENSE.md ├── .eslintrc.js ├── .travis.yml ├── package.json ├── addon └── services │ └── storage.js └── README.md /vendor/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/helpers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/models/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/controllers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.watchmanconfig: -------------------------------------------------------------------------------- 1 | { 2 | "ignore_dirs": ["tmp", "dist"] 3 | } 4 | -------------------------------------------------------------------------------- /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "bower_components", 3 | "analytics": false 4 | } 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | name: 'ember-storage' 5 | }; 6 | -------------------------------------------------------------------------------- /tests/dummy/public/robots.txt: -------------------------------------------------------------------------------- 1 | # http://www.robotstxt.org 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/application.hbs: -------------------------------------------------------------------------------- 1 |

Welcome to Ember.js

2 | 3 | {{outlet}} -------------------------------------------------------------------------------- /tests/dummy/app/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from 'ember-resolver'; 2 | 3 | export default Resolver; 4 | -------------------------------------------------------------------------------- /app/services/storage.js: -------------------------------------------------------------------------------- 1 | import storage from 'ember-storage/services/storage'; 2 | 3 | export default storage; 4 | -------------------------------------------------------------------------------- /config/environment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(/* environment, appConfig */) { 4 | return { }; 5 | }; 6 | -------------------------------------------------------------------------------- /tests/test-helper.js: -------------------------------------------------------------------------------- 1 | import Application from '../app'; 2 | import config from '../config/environment'; 3 | import { setApplication } from '@ember/test-helpers'; 4 | import { start } from 'ember-qunit'; 5 | 6 | setApplication(Application.create(config.APP)); 7 | 8 | start(); 9 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/index.hbs: -------------------------------------------------------------------------------- 1 | 2 | This is a localStorage demo. Open several tabs to this page, move your mouse 3 | around, and fill out the form. 4 | 5 | {{storage-demo}} 6 | 7 |

8 | Download the code 9 |

10 | 11 | -------------------------------------------------------------------------------- /tests/dummy/app/router.js: -------------------------------------------------------------------------------- 1 | import EmberRouter from '@ember/routing/router'; 2 | import config from './config/environment'; 3 | 4 | const Router = EmberRouter.extend({ 5 | location: config.locationType, 6 | rootURL: config.rootURL 7 | }); 8 | 9 | Router.map(function() { 10 | }); 11 | 12 | export default Router; 13 | -------------------------------------------------------------------------------- /.ember-cli: -------------------------------------------------------------------------------- 1 | { 2 | /** 3 | Ember CLI sends analytics information by default. The data is completely 4 | anonymous, but there are times when you might want to disable this behavior. 5 | 6 | Setting `disableAnalytics` to true will prevent any data from being sent. 7 | */ 8 | "disableAnalytics": false 9 | } 10 | -------------------------------------------------------------------------------- /tests/helpers/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from 'ember/resolver'; 2 | import config from '../../config/environment'; 3 | 4 | var resolver = Resolver.create(); 5 | 6 | resolver.namespace = { 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix 9 | }; 10 | 11 | export default resolver; 12 | -------------------------------------------------------------------------------- /app/initializers/storage-service.js: -------------------------------------------------------------------------------- 1 | export function initialize() { 2 | let application = arguments[1] || arguments[0]; 3 | application.inject('route', 'storage', 'service:storage'); 4 | application.inject('component', 'storage', 'service:storage'); 5 | } 6 | 7 | export default { 8 | name: 'storage-service', 9 | initialize: initialize 10 | }; 11 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /bower_components 2 | /config/ember-try.js 3 | /dist 4 | /tests 5 | /tmp 6 | **/.gitkeep 7 | .bowerrc 8 | .editorconfig 9 | .ember-cli 10 | .eslintrc.js 11 | .gitignore 12 | .watchmanconfig 13 | .travis.yml 14 | bower.json 15 | ember-cli-build.js 16 | testem.js 17 | 18 | # ember-try 19 | .node_modules.ember-try/ 20 | bower.json.ember-try 21 | package.json.ember-try 22 | -------------------------------------------------------------------------------- /tests/dummy/config/targets.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const browsers = [ 4 | 'last 1 Chrome versions', 5 | 'last 1 Firefox versions', 6 | 'last 1 Safari versions' 7 | ]; 8 | 9 | const isCI = !!process.env.CI; 10 | const isProduction = process.env.EMBER_ENV === 'production'; 11 | 12 | if (isCI || isProduction) { 13 | browsers.push('ie 11'); 14 | } 15 | 16 | module.exports = { 17 | browsers 18 | }; 19 | -------------------------------------------------------------------------------- /tests/dummy/app/app.js: -------------------------------------------------------------------------------- 1 | import Application from '@ember/application'; 2 | import Resolver from './resolver'; 3 | import loadInitializers from 'ember-load-initializers'; 4 | import config from './config/environment'; 5 | 6 | const App = Application.extend({ 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix, 9 | Resolver 10 | }); 11 | 12 | loadInitializers(App, config.modulePrefix); 13 | 14 | export default App; 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | 7 | # dependencies 8 | /node_modules 9 | /bower_components 10 | 11 | # misc 12 | /.sass-cache 13 | /connect.lock 14 | /coverage/* 15 | /libpeerconnection.log 16 | npm-debug.log* 17 | yarn-error.log 18 | testem.log 19 | 20 | # ember-try 21 | .node_modules.ember-try/ 22 | bower.json.ember-try 23 | package.json.ember-try 24 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/storage-demo.hbs: -------------------------------------------------------------------------------- 1 | 2 |

3 | This form remembers everything you type and syncs it between tabs: 4 |

5 | 6 |
7 |
8 | 9 | {{input value=storage.name}} 10 |
11 | 12 |
13 | 14 | {{input value=storage.email}} 15 |
16 | 17 |
18 | 19 | {{textarea value=storage.message}} 20 |
21 |
22 | 23 |
24 |
25 |
26 | -------------------------------------------------------------------------------- /tests/dummy/app/styles/app.css: -------------------------------------------------------------------------------- 1 | 2 | body, html { 3 | overflow: hidden; 4 | } 5 | label { 6 | width: 100px; 7 | line-height: 60px; 8 | } 9 | input, textarea { 10 | width: 300px; 11 | float: right; 12 | margin: 20px; 13 | } 14 | textarea { 15 | height: 200px; 16 | } 17 | div { 18 | width: 100%; 19 | float: left; 20 | } 21 | 22 | #spot { 23 | position:absolute; 24 | } 25 | 26 | #spot div { 27 | width:30px; 28 | height:30px; 29 | background:yellow; 30 | opacity:0.4; 31 | border-radius:50%; 32 | margin: -4px 0 0 -4px; 33 | } 34 | -------------------------------------------------------------------------------- /ember-cli-build.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const EmberAddon = require('ember-cli/lib/broccoli/ember-addon'); 4 | 5 | module.exports = function(defaults) { 6 | let app = new EmberAddon(defaults, { 7 | // Add options here 8 | }); 9 | 10 | /* 11 | This build file specifies the options for the dummy test app of this 12 | addon, located in `/tests/dummy` 13 | This build file does *not* influence how the addon or the app using it 14 | behave. You most likely want to be modifying `./index.js` or app's build file 15 | */ 16 | 17 | return app.toTree(); 18 | }; 19 | -------------------------------------------------------------------------------- /testem.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | test_page: 'tests/index.html?hidepassed', 3 | disable_watching: true, 4 | launch_in_ci: [ 5 | 'Chrome' 6 | ], 7 | launch_in_dev: [ 8 | 'Chrome' 9 | ], 10 | browser_args: { 11 | Chrome: { 12 | mode: 'ci', 13 | args: [ 14 | // --no-sandbox is needed when running Chrome inside a container 15 | process.env.TRAVIS ? '--no-sandbox' : null, 16 | 17 | '--disable-gpu', 18 | '--headless', 19 | '--remote-debugging-port=0', 20 | '--window-size=1440,900' 21 | ].filter(Boolean) 22 | } 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": [ 3 | "document", 4 | "window", 5 | "-Promise" 6 | ], 7 | "browser": true, 8 | "boss": true, 9 | "curly": true, 10 | "debug": false, 11 | "devel": true, 12 | "eqeqeq": true, 13 | "evil": true, 14 | "forin": false, 15 | "immed": false, 16 | "laxbreak": false, 17 | "newcap": true, 18 | "noarg": true, 19 | "noempty": false, 20 | "nonew": false, 21 | "nomen": false, 22 | "onevar": false, 23 | "plusplus": false, 24 | "regexp": false, 25 | "undef": true, 26 | "sub": true, 27 | "strict": false, 28 | "white": false, 29 | "eqnull": true, 30 | "esnext": true, 31 | "unused": true 32 | } 33 | -------------------------------------------------------------------------------- /tests/dummy/public/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 16 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [*.js] 17 | indent_style = space 18 | indent_size = 2 19 | 20 | [*.hbs] 21 | insert_final_newline = false 22 | indent_style = space 23 | indent_size = 2 24 | 25 | [*.css] 26 | indent_style = space 27 | indent_size = 2 28 | 29 | [*.html] 30 | indent_style = space 31 | indent_size = 2 32 | 33 | [*.{diff,md}] 34 | trim_trailing_whitespace = false 35 | -------------------------------------------------------------------------------- /tests/dummy/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Ember Storage 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | 12 | 13 | 14 | 15 | {{content-for "head-footer"}} 16 | 17 | 18 | {{content-for "body"}} 19 | 20 | 21 | 22 | 23 | {{content-for "body-footer"}} 24 | 25 | 26 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parserOptions: { 4 | ecmaVersion: 2017, 5 | sourceType: 'module' 6 | }, 7 | plugins: [ 8 | 'ember' 9 | ], 10 | extends: [ 11 | 'eslint:recommended', 12 | 'plugin:ember/recommended' 13 | ], 14 | env: { 15 | browser: true 16 | }, 17 | rules: { 18 | }, 19 | overrides: [ 20 | // node files 21 | { 22 | files: [ 23 | 'ember-cli-build.js', 24 | 'index.js', 25 | 'testem.js', 26 | 'config/**/*.js', 27 | 'tests/dummy/config/**/*.js' 28 | ], 29 | excludedFiles: [ 30 | 'addon/**', 31 | 'addon-test-support/**', 32 | 'app/**', 33 | 'tests/dummy/app/**' 34 | ], 35 | parserOptions: { 36 | sourceType: 'script', 37 | ecmaVersion: 2015 38 | }, 39 | env: { 40 | browser: false, 41 | node: true 42 | }, 43 | plugins: ['node'], 44 | rules: Object.assign({}, require('eslint-plugin-node').configs.recommended.rules, { 45 | // add your custom rules and overrides for node files here 46 | }) 47 | } 48 | ] 49 | }; 50 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy Tests 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | {{content-for "test-head"}} 12 | 13 | 14 | 15 | 16 | 17 | {{content-for "head-footer"}} 18 | {{content-for "test-head-footer"}} 19 | 20 | 21 | {{content-for "body"}} 22 | {{content-for "test-body"}} 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | {{content-for "body-footer"}} 31 | {{content-for "test-body-footer"}} 32 | 33 | 34 | -------------------------------------------------------------------------------- /tests/dummy/app/components/storage-demo.js: -------------------------------------------------------------------------------- 1 | import Component from '@ember/component'; 2 | import { computed } from '@ember/object'; 3 | import { throttle } from '@ember/runloop'; 4 | import { htmlSafe } from "@ember/string"; 5 | 6 | export default Component.extend({ 7 | 8 | style: computed('storage.{mouseX,mouseY}', function() { 9 | const mouseY = this.get('storage.mouseY'); 10 | const mouseX = this.get('storage.mouseX'); 11 | return new htmlSafe(`left:${mouseX}px; top:${mouseY}px`); 12 | }), 13 | 14 | didInsertElement() { 15 | this._super(...arguments); 16 | 17 | window.onmousemove = (e) => { 18 | this.x = e.x+1; 19 | this.y = e.y+1; 20 | 21 | if ( ! window.requestAnimationFrame ) { 22 | throttle(this, this.updateLocation, 100); 23 | } 24 | }; 25 | 26 | if (window.requestAnimationFrame) { 27 | this.updateLocation(); 28 | } 29 | }, 30 | 31 | updateLocation: function() { 32 | if (this.oldX !== this.x || this.oldY !== this.y) { 33 | this.storage.set('mouseX', this.x); 34 | this.storage.set('mouseY', this.y); 35 | this.oldX = this.x; 36 | this.oldY = this.y; 37 | } 38 | 39 | if (window.requestAnimationFrame) { 40 | window.requestAnimationFrame(this.updateLocation.bind(this)); 41 | } 42 | } 43 | 44 | }); 45 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | node_js: 4 | # we recommend testing addons with the same minimum supported node version as Ember CLI 5 | # so that your addon works for all apps 6 | - "4" 7 | 8 | sudo: false 9 | dist: trusty 10 | 11 | addons: 12 | chrome: stable 13 | 14 | cache: 15 | directories: 16 | - $HOME/.npm 17 | 18 | env: 19 | global: 20 | # See https://git.io/vdao3 for details. 21 | - JOBS=1 22 | matrix: 23 | # we recommend new addons test the current and previous LTS 24 | # as well as latest stable release (bonus points to beta/canary) 25 | - EMBER_TRY_SCENARIO=ember-lts-2.12 26 | - EMBER_TRY_SCENARIO=ember-lts-2.16 27 | - EMBER_TRY_SCENARIO=ember-lts-2.18 28 | - EMBER_TRY_SCENARIO=ember-release 29 | - EMBER_TRY_SCENARIO=ember-beta 30 | - EMBER_TRY_SCENARIO=ember-canary 31 | - EMBER_TRY_SCENARIO=ember-default 32 | 33 | matrix: 34 | fast_finish: true 35 | allow_failures: 36 | - env: EMBER_TRY_SCENARIO=ember-canary 37 | 38 | before_install: 39 | - npm config set spin false 40 | - npm install -g npm@4 41 | - npm --version 42 | 43 | script: 44 | - npm run lint:js 45 | # Usually, it's ok to finish the test scenario without reverting 46 | # to the addon's original dependency state, skipping "cleanup". 47 | - node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO --skip-cleanup 48 | -------------------------------------------------------------------------------- /tests/dummy/config/environment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(environment) { 4 | let ENV = { 5 | modulePrefix: 'dummy', 6 | environment, 7 | rootURL: '/', 8 | locationType: 'auto', 9 | EmberENV: { 10 | FEATURES: { 11 | // Here you can enable experimental features on an ember canary build 12 | // e.g. 'with-controller': true 13 | }, 14 | EXTEND_PROTOTYPES: { 15 | // Prevent Ember Data from overriding Date.parse. 16 | Date: false 17 | } 18 | }, 19 | 20 | APP: { 21 | // Here you can pass flags/options to your application instance 22 | // when it is created 23 | } 24 | }; 25 | 26 | if (environment === 'development') { 27 | // ENV.APP.LOG_RESOLVER = true; 28 | // ENV.APP.LOG_ACTIVE_GENERATION = true; 29 | // ENV.APP.LOG_TRANSITIONS = true; 30 | // ENV.APP.LOG_TRANSITIONS_INTERNAL = true; 31 | // ENV.APP.LOG_VIEW_LOOKUPS = true; 32 | } 33 | 34 | if (environment === 'test') { 35 | // Testem prefers this... 36 | ENV.locationType = 'none'; 37 | 38 | // keep test console output quieter 39 | ENV.APP.LOG_ACTIVE_GENERATION = false; 40 | ENV.APP.LOG_VIEW_LOOKUPS = false; 41 | 42 | ENV.APP.rootElement = '#ember-testing'; 43 | ENV.APP.autoboot = false; 44 | } 45 | 46 | if (environment === 'production') { 47 | // here you can enable a production-specific feature 48 | } 49 | 50 | return ENV; 51 | }; 52 | -------------------------------------------------------------------------------- /config/ember-try.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const getChannelURL = require('ember-source-channel-url'); 4 | 5 | module.exports = function() { 6 | return Promise.all([ 7 | getChannelURL('release'), 8 | getChannelURL('beta'), 9 | getChannelURL('canary') 10 | ]).then((urls) => { 11 | return { 12 | scenarios: [ 13 | { 14 | name: 'ember-lts-2.12', 15 | npm: { 16 | devDependencies: { 17 | 'ember-source': '~2.12.0' 18 | } 19 | } 20 | }, 21 | { 22 | name: 'ember-lts-2.16', 23 | npm: { 24 | devDependencies: { 25 | 'ember-source': '~2.16.0' 26 | } 27 | } 28 | }, 29 | { 30 | name: 'ember-lts-2.18', 31 | npm: { 32 | devDependencies: { 33 | 'ember-source': '~2.18.0' 34 | } 35 | } 36 | }, 37 | { 38 | name: 'ember-release', 39 | npm: { 40 | devDependencies: { 41 | 'ember-source': urls[0] 42 | } 43 | } 44 | }, 45 | { 46 | name: 'ember-beta', 47 | npm: { 48 | devDependencies: { 49 | 'ember-source': urls[1] 50 | } 51 | } 52 | }, 53 | { 54 | name: 'ember-canary', 55 | npm: { 56 | devDependencies: { 57 | 'ember-source': urls[2] 58 | } 59 | } 60 | }, 61 | { 62 | name: 'ember-default', 63 | npm: { 64 | devDependencies: {} 65 | } 66 | } 67 | ] 68 | }; 69 | }); 70 | }; 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-storage", 3 | "version": "2.0.0", 4 | "description": "Store data in localStorage or sessionStorage and synchronize it between tabs.", 5 | "directories": { 6 | "doc": "doc", 7 | "test": "tests" 8 | }, 9 | "scripts": { 10 | "build": "ember build", 11 | "lint:js": "eslint ./*.js addon addon-test-support app config lib server test-support tests", 12 | "start": "ember serve", 13 | "test": "ember test", 14 | "test:all": "ember try:each" 15 | }, 16 | "repository": "https://github.com/jerel/ember-storage", 17 | "engines": { 18 | "node": "^4.5 || 6.* || >= 7.*" 19 | }, 20 | "author": "Jerel Unruh", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/jerel/ember-storage/issues" 24 | }, 25 | "homepage": "http://storage.jerel.co", 26 | "dependencies": { 27 | "ember-cli-babel": "^6.6.0" 28 | }, 29 | "devDependencies": { 30 | "broccoli-asset-rev": "^2.4.5", 31 | "ember-cli": "~3.1.2", 32 | "ember-cli-dependency-checker": "^2.0.0", 33 | "ember-cli-eslint": "^4.2.1", 34 | "ember-cli-htmlbars": "^2.0.1", 35 | "ember-cli-htmlbars-inline-precompile": "^1.0.0", 36 | "ember-cli-inject-live-reload": "^1.4.1", 37 | "ember-cli-qunit": "^4.1.1", 38 | "ember-cli-shims": "^1.2.0", 39 | "ember-cli-sri": "^2.1.0", 40 | "ember-cli-uglify": "^2.0.0", 41 | "ember-disable-prototype-extensions": "^1.1.2", 42 | "ember-export-application-global": "^2.0.0", 43 | "ember-load-initializers": "^1.0.0", 44 | "ember-maybe-import-regenerator": "^0.1.6", 45 | "ember-resolver": "^4.0.0", 46 | "ember-source": "~3.1.0", 47 | "ember-source-channel-url": "^1.0.1", 48 | "ember-try": "^0.2.23", 49 | "eslint-plugin-ember": "^5.0.0", 50 | "eslint-plugin-node": "^6.0.1", 51 | "loader.js": "^4.2.3" 52 | }, 53 | "keywords": [ 54 | "ember-addon", 55 | "localStorage", 56 | "sessionStorage", 57 | "sync", 58 | "tab synchronization", 59 | "ember" 60 | ], 61 | "ember-addon": { 62 | "configPath": "tests/dummy/config", 63 | "versionCompatibility": { 64 | "ember": ">=2.12.0" 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /addon/services/storage.js: -------------------------------------------------------------------------------- 1 | import Service from '@ember/service'; 2 | import { isNone } from '@ember/utils'; 3 | 4 | var storage = { 5 | 'local': window.localStorage, 6 | 'session': window.sessionStorage, 7 | }; 8 | 9 | export default Service.extend({ 10 | 11 | prefix: 'es', 12 | type: 'local', 13 | 14 | _prefix(key) { 15 | return this.get('prefix')+'__'+key; 16 | }, 17 | 18 | init() { 19 | this._super(...arguments); 20 | 21 | let self = this, 22 | regexp = new RegExp('^('+this.get('prefix')+'__)'); 23 | 24 | this._notify = function(evnt) { 25 | self.notifyPropertyChange(evnt.key.replace(regexp, '')); 26 | }; 27 | 28 | window.addEventListener('storage', this._notify, false); 29 | }, 30 | 31 | willDestroy() { 32 | this._super(...arguments); 33 | window.removeEventListener('storage', this._notify, false); 34 | }, 35 | 36 | unknownProperty(k) { 37 | var key = this._prefix(k), 38 | type = this.get('type'); 39 | // if we don't use JSON.parse here then observing a boolean doesn't work 40 | return storage[type][key] && JSON.parse(storage[type][key]); 41 | }, 42 | setUnknownProperty(k, value) { 43 | let key = this._prefix(k), 44 | type = this.get('type'); 45 | 46 | if(isNone(value)) { 47 | delete storage[type][key]; 48 | } else { 49 | storage[type][key] = JSON.stringify(value); 50 | } 51 | this.notifyPropertyChange(k); 52 | return value; 53 | }, 54 | clear(keyPrefix) { 55 | this.beginPropertyChanges(); 56 | let prefix = keyPrefix || this.get('prefix'), 57 | regexp = new RegExp('^('+prefix+'__)'), 58 | type = this.get('type'), 59 | toDelete = []; 60 | 61 | for (var i=0; i < storage[type].length; i++){ 62 | let key = storage[type].key(i); 63 | // don't nuke *everything* in localStorage... just keys that match our pattern 64 | if (key.match(regexp)) { 65 | toDelete.push(key); 66 | } 67 | } 68 | toDelete.forEach(function(key) { 69 | delete storage[type][key]; 70 | key = key.replace(regexp, ''); 71 | this.set(key); 72 | }, this); 73 | this.endPropertyChanges(); 74 | } 75 | }); 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ember Storage 2 | 3 | A simple but powerful way for managing data that you wish to last through 4 | a page reload. This add-on synchronizes data between browser tabs when using 5 | localStorage and it is 6 | observable. 7 | 8 | [Live Demo](http://storage.jerel.co/) 9 | 10 | [![Build Status](https://travis-ci.org/jerel/ember-storage.svg?branch=master)](https://travis-ci.org/jerel/ember-storage) 11 | 12 | ## Installation 13 | 14 | * `ember install:addon ember-storage` 15 | 16 | ## Usage 17 | 18 | This service is injected into all routes and components as `storage`. You 19 | may get, set, and observe data on `storage` just like a regular Ember Object: 20 | 21 | ````js 22 | export default Ember.Component.extend({ 23 | fullWidth: function() { 24 | return !this.get('storage.sideBarOpen'); 25 | }.property('storage.sideBarOpen'), 26 | actions: { 27 | toggleMenu: function() { 28 | this.toggleProperty('storage.sideBarOpen'); 29 | }, 30 | }, 31 | }); 32 | ```` 33 | ````html 34 | 35 | 38 | 39 | 40 | ```` 41 | ## API 42 | 43 | * `prefix` (property). Set a key prefix. Handy for saving user specific device config. Defaults to 'es'. 44 | * `type` (property). Either 'session' or 'local'. Defaults to 'local'. Due to the way `sessionStorage` works tab sync does not work if type is set to 'session'. 45 | * `clear` (function). Clear all data for the specified key. Defaults to the key currently set in `prefix`. 46 | 47 | Examples: 48 | ````js 49 | // application route 50 | export default Ember.Route.extend({ 51 | afterModel: function(model) { 52 | this.currentUserID = model.id; 53 | // now multiple users could use this device without sharing data 54 | this.storage.set('prefix', this.currentUserID); 55 | }, 56 | actions: { 57 | resetUserPreferences: function() { 58 | this.storage.clear(this.currentUserID); 59 | }, 60 | }, 61 | }); 62 | ```` 63 | To create an additional instance (maybe one for sessionStorage) add an initializer to your app: 64 | ````js 65 | // app/initializers/session-service.js 66 | import Session from '../services/storage'; 67 | 68 | var session = Session.create({ 69 | type: 'session', 70 | }); 71 | 72 | export function initialize(container, application) { 73 | container.register('service:session', session, {instantiate: false}); 74 | application.inject('route', 'session', 'service:session'); 75 | application.inject('component', 'session', 'service:session'); 76 | } 77 | 78 | export default { 79 | name: 'session-service', 80 | initialize: initialize 81 | }; 82 | ```` 83 | ## Running Tests 84 | 85 | * `ember test` 86 | * `ember test --server` 87 | 88 | For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/). 89 | -------------------------------------------------------------------------------- /tests/unit/services/storage-test.js: -------------------------------------------------------------------------------- 1 | import { module, test } from 'qunit'; 2 | import { setupTest } from 'ember-qunit'; 3 | 4 | 5 | module('Unit | Service | storage', function(hooks) { 6 | setupTest(hooks); 7 | 8 | // setup 9 | hooks.beforeEach(function() { 10 | const service = this.owner.lookup('service:storage'); 11 | service.set('prefix', 'test-temp'); 12 | service.set('type', 'session'); 13 | }); 14 | 15 | // Replace this with your real tests. 16 | test('prefix exists and can be set', function(assert) { 17 | const service = this.owner.lookup('service:storage'); 18 | assert.equal(service.get('prefix'), 'test-temp'); 19 | service.set('prefix', 'test'); 20 | assert.equal(service.get('prefix'), 'test'); 21 | }); 22 | 23 | test('set prefix is applied to key', function(assert) { 24 | const service = this.owner.lookup('service:storage'); 25 | assert.equal(service._prefix('my-key'), 'test-temp__my-key'); 26 | }); 27 | 28 | test('integers can be set', function(assert) { 29 | const service = this.owner.lookup('service:storage'); 30 | service.set('age', 23); 31 | assert.equal(window.sessionStorage['test-temp__age'], 23); 32 | }); 33 | 34 | test('strings can be set', function(assert) { 35 | const service = this.owner.lookup('service:storage'); 36 | service.set('name', 'forrest'); 37 | assert.equal(window.sessionStorage['test-temp__name'], JSON.stringify('forrest')); 38 | assert.equal(service.get('name'), 'forrest'); 39 | }); 40 | 41 | test('objects can be set', function(assert) { 42 | const service = this.owner.lookup('service:storage'); 43 | 44 | let user = { 45 | name: 'forrest', 46 | weight: 170, 47 | }; 48 | service.set('user', user); 49 | assert.equal(window.sessionStorage['test-temp__user'], JSON.stringify(user)); 50 | assert.equal(JSON.stringify(service.get('user')), JSON.stringify(user)); 51 | }); 52 | 53 | test('arrays can be set', function(assert) { 54 | const service = this.owner.lookup('service:storage'); 55 | 56 | let friends = [ 57 | 'jenny', 58 | ]; 59 | service.set('friends', friends); 60 | assert.equal(window.sessionStorage['test-temp__friends'], JSON.stringify(friends)); 61 | assert.equal(JSON.stringify(service.get('friends')), JSON.stringify(friends)); 62 | }); 63 | 64 | test('items set by this service can be cleared', function(assert) { 65 | assert.expect(1); 66 | 67 | const service = this.owner.lookup('service:storage'); 68 | let length = window.sessionStorage.length; 69 | 70 | service.clear(); 71 | assert.ok(length > window.sessionStorage.length); 72 | for (var i=0; i < window.sessionStorage.length; i++) { 73 | var key = window.sessionStorage.key(i); 74 | if (key && key.match(/^test-temp__.*?/)) { 75 | assert.ok(false, 'Undeleted item in storage'); 76 | } 77 | } 78 | }); 79 | 80 | }); 81 | 82 | --------------------------------------------------------------------------------