├── example ├── packages │ └── stored-var ├── .meteor │ ├── .gitignore │ ├── release │ ├── platforms │ ├── .finished-upgraders │ ├── packages │ ├── .id │ └── versions ├── example.html └── example.js ├── .gitignore ├── .versions ├── README.md ├── package.js ├── LICENSE.md └── stored_var.js /example/packages/stored-var: -------------------------------------------------------------------------------- 1 | ../../ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | .idea/ 3 | -------------------------------------------------------------------------------- /example/.meteor/.gitignore: -------------------------------------------------------------------------------- 1 | local 2 | -------------------------------------------------------------------------------- /example/.meteor/release: -------------------------------------------------------------------------------- 1 | METEOR@1.0.2.1 2 | -------------------------------------------------------------------------------- /example/.meteor/platforms: -------------------------------------------------------------------------------- 1 | server 2 | browser 3 | -------------------------------------------------------------------------------- /.versions: -------------------------------------------------------------------------------- 1 | base64@1.0.2 2 | dispatch:stored-var@1.0.0 3 | ejson@1.0.5 4 | json@1.0.2 5 | localstorage@1.0.2 6 | meteor@1.1.4 7 | random@1.0.2 8 | reactive-var@1.0.4 9 | tracker@1.0.4 10 | underscore@1.0.2 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | StoredVar 2 | ============= 3 | 4 | Cache a reactive var on localStorage. 5 | 6 | ##Usage 7 | 8 | `meteor add dispatch:stored-var` 9 | 10 | ``` 11 | var storedVar = new StoredVar('localStorageKeyName'); 12 | storedVar.set('My cached key'); 13 | ``` 14 | -------------------------------------------------------------------------------- /example/.meteor/.finished-upgraders: -------------------------------------------------------------------------------- 1 | # This file contains information which helps Meteor properly upgrade your 2 | # app when you run 'meteor update'. You should check it into version control 3 | # with your project. 4 | 5 | notices-for-0.9.0 6 | notices-for-0.9.1 7 | 0.9.4-platform-file 8 | -------------------------------------------------------------------------------- /example/example.html: -------------------------------------------------------------------------------- 1 |
2 |The stored var value is {{number}}. Refresh the page to see that it has been cached.
11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /example/.meteor/packages: -------------------------------------------------------------------------------- 1 | # Meteor packages used by this project, one per line. 2 | # Check this file (and the other files in this directory) into your repository. 3 | # 4 | # 'meteor add' and 'meteor remove' will edit this file for you, 5 | # but you can also edit it by hand. 6 | 7 | meteor-platform 8 | momentjs:moment 9 | dispatch:stored-var 10 | -------------------------------------------------------------------------------- /example/.meteor/.id: -------------------------------------------------------------------------------- 1 | # This file contains a token that is unique to your project. 2 | # Check it into your repository along with the rest of this directory. 3 | # It can be used for purposes such as: 4 | # - ensuring you don't accidentally deploy one app on top of another 5 | # - providing package authors with aggregated statistics 6 | 7 | mu3ecubt3iqucodjo8 8 | -------------------------------------------------------------------------------- /example/example.js: -------------------------------------------------------------------------------- 1 | if (Meteor.isClient) { 2 | var storedNumber = new StoredVar('storedNumber'); 3 | if (!storedNumber.get()) storedNumber.set(0); 4 | 5 | Template.example.events({ 6 | 'click button': function () { 7 | storedNumber.set(storedNumber.get() + 1); 8 | } 9 | }); 10 | 11 | Template.example.helpers({ 12 | number: storedNumber.get 13 | }); 14 | } 15 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'dispatch:stored-var', 3 | summary: 'A ReactiveVar cached on localStorage.', 4 | version: '1.0.0', 5 | git: 'https://github.com/DispatchMe/meteor-stored-var.git' 6 | }); 7 | 8 | Package.onUse(function (api) { 9 | api.versionsFrom('1.0'); 10 | 11 | api.use(['ejson','localstorage', 'reactive-var'], 'web'); 12 | 13 | api.addFiles('stored_var.js', 'web'); 14 | 15 | api.export('StoredVar', 'web'); 16 | }); 17 | -------------------------------------------------------------------------------- /example/.meteor/versions: -------------------------------------------------------------------------------- 1 | application-configuration@1.0.4 2 | autoupdate@1.1.4 3 | base64@1.0.2 4 | binary-heap@1.0.2 5 | blaze@2.0.4 6 | blaze-tools@1.0.2 7 | boilerplate-generator@1.0.2 8 | callback-hook@1.0.2 9 | check@1.0.3 10 | ddp@1.0.13 11 | deps@1.0.6 12 | dispatch:stored-var@1.0.0 13 | ejson@1.0.5 14 | fastclick@1.0.2 15 | follower-livedata@1.0.3 16 | geojson-utils@1.0.2 17 | html-tools@1.0.3 18 | htmljs@1.0.3 19 | http@1.0.9 20 | id-map@1.0.2 21 | jquery@1.0.2 22 | json@1.0.2 23 | launch-screen@1.0.1 24 | livedata@1.0.12 25 | localstorage@1.0.2 26 | logging@1.0.6 27 | meteor@1.1.4 28 | meteor-platform@1.2.1 29 | minifiers@1.1.3 30 | minimongo@1.0.6 31 | mobile-status-bar@1.0.2 32 | momentjs:moment@2.9.0 33 | mongo@1.0.11 34 | observe-sequence@1.0.4 35 | ordered-dict@1.0.2 36 | random@1.0.2 37 | reactive-dict@1.0.5 38 | reactive-var@1.0.4 39 | reload@1.1.2 40 | retry@1.0.2 41 | routepolicy@1.0.3 42 | session@1.0.5 43 | spacebars@1.0.4 44 | spacebars-compiler@1.0.4 45 | templating@1.0.10 46 | tracker@1.0.4 47 | ui@1.0.5 48 | underscore@1.0.2 49 | url@1.0.3 50 | webapp@1.1.5 51 | webapp-hashing@1.0.2 52 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Dispatch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /stored_var.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ## [new] StoredVar(localStorageKey) 3 | * 4 | * A cached ReactiveVar on localStorage. 5 | * 6 | * @param localStorageKey 7 | * @constructor 8 | */ 9 | StoredVar = function (localStorageKey) { 10 | if (!(this instanceof StoredVar)) 11 | // called without `new` 12 | return new StoredVar(localStorageKey); 13 | 14 | this.key = localStorageKey; 15 | 16 | // We use an internal ReactiveVar instead of directly accessing 17 | // localStorage for performance because localStorage is slow. 18 | this.var = new ReactiveVar(); 19 | 20 | var value = Meteor._localStorage.getItem(localStorageKey); 21 | if (value) this.var.set(EJSON.parse(value)); 22 | 23 | // Bind the functions to this so they can 24 | // be used directly as template helpers 25 | this.get = this.get.bind(this); 26 | this.set = this.set.bind(this); 27 | this.clear = this.clear.bind(this); 28 | }; 29 | 30 | /** 31 | * Get the stored value (reactive). 32 | * @returns {*|any} 33 | */ 34 | StoredVar.prototype.get = function () { 35 | return this.var.get(); 36 | }; 37 | 38 | /** 39 | * Set and cache the value. 40 | * @returns {*|any} 41 | */ 42 | StoredVar.prototype.set = function (val) { 43 | this.var.set(val); 44 | Meteor._localStorage.setItem(this.key, EJSON.stringify(val)); 45 | }; 46 | 47 | /** 48 | * Clear the value. 49 | */ 50 | StoredVar.prototype.clear = function () { 51 | this.var.set(); 52 | Meteor._localStorage.removeItem(this.key); 53 | }; 54 | --------------------------------------------------------------------------------